@@ -6,11 +6,14 @@ defmodule DeeplEx do
66 alias DeeplEx.Configuration
77 alias DeeplEx.DeepL
88 alias DeeplEx.LanguageValidator
9+ alias DeeplEx.OptionsValidator
910
1011 @ doc """
1112 This function translates the given text from the given source language, to the given target language.
1213 It either returns `{:ok, translation}` or `{:error, error}`.
1314
15+ See `DeeplEx.LanguageValidator` and `DeeplEx.OptionsValidator` for more information about the allowed values.
16+
1417 ## Example with correct input
1518
1619
@@ -26,28 +29,42 @@ defmodule DeeplEx do
2629 iex> DeeplEx.translate("Hoje vou comer.", :ZZ, :EN)
2730 {:error, :invalid_language_specification}
2831 ```
32+
33+ ## Example when using options
34+
35+ ```elixir
36+ iex> DeeplEx.translate("Hello, how are you?", :EN, :PT_BR, %{formality: "less"})
37+ {:ok, "Olá, cara, como você está?"}
38+ ```
2939 """
3040 def translate (
3141 text ,
3242 source_language ,
33- target_language
43+ target_language ,
44+ options \\ % { }
3445 )
3546 when is_atom ( source_language ) and is_atom ( target_language ) and is_binary ( text ) do
3647 with { :valid_source_language? , true } <-
3748 LanguageValidator . valid_source_language? ( source_language ) ,
3849 { :valid_target_language? , true } <-
3950 LanguageValidator . valid_target_language? ( target_language ) ,
51+ { :valid_options? , true } <-
52+ OptionsValidator . valid_options? ( options ) ,
4053 { :ok , api_key } = Configuration . api_key ( ) ,
4154 tier = Configuration . tier ( api_key ) ,
4255 client <- DeepL . client ( api_key , tier ) ,
4356 { :ok , % { body: % { "translations" => [ % { "text" => translation } ] } } } <-
4457 DeepL . translate ( client , % {
4558 source_language: source_language ,
4659 target_language: target_language ,
47- text: text
60+ text: text ,
61+ options: options
4862 } ) do
4963 { :ok , translation }
5064 else
65+ { :ok , % { status: 400 , body: % { "message" => message } } } ->
66+ { :error , message }
67+
5168 error ->
5269 error_response ( error )
5370 end
@@ -57,5 +74,9 @@ defmodule DeeplEx do
5774 when error in [ :valid_source_language? , :valid_target_language? ] ,
5875 do: { :error , :invalid_language_specification }
5976
77+ defp error_response ( { error , _ } )
78+ when error == :valid_options? ,
79+ do: { :error , :invalid_options_specification }
80+
6081 defp error_response ( error ) , do: error
6182end
0 commit comments