Skip to content

Commit 73555ba

Browse files
authored
Merge pull request #31 from hergetto/development
[Chore]: Release v0.2.0
2 parents be79632 + 39c67ee commit 73555ba

12 files changed

Lines changed: 94 additions & 30 deletions

.github/workflows/dependencies-reusable.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
runs-on: ${{ inputs.os }}
2121

2222
steps:
23-
- uses: actions/checkout@v3
23+
- uses: actions/checkout@v4
2424

2525
- name: Setup Elixir
2626
uses: erlef/setup-beam@v1.16.0

.github/workflows/linting-reusable.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
runs-on: ${{ inputs.os }}
2121

2222
steps:
23-
- uses: actions/checkout@v3
23+
- uses: actions/checkout@v4
2424

2525
- name: Setup Elixir
2626
uses: erlef/setup-beam@v1.16.0

.github/workflows/pr-title-checker.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
name: Check title of PR
1212
runs-on: ubuntu-latest
1313
steps:
14-
- uses: thehanimo/pr-title-checker@v1.4.0
14+
- uses: thehanimo/pr-title-checker@v1.4.1
1515
with:
1616
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1717
pass_on_octokit_error: false

.github/workflows/publish-to-hex-reusable.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
runs-on: ${{ inputs.os }}
3030

3131
steps:
32-
- uses: actions/checkout@v3
32+
- uses: actions/checkout@v4
3333

3434
- name: Setup Elixir
3535
uses: erlef/setup-beam@v1.16.0

.github/workflows/static-code-analysis-reusable.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
runs-on: ${{ inputs.os }}
2121

2222
steps:
23-
- uses: actions/checkout@v3
23+
- uses: actions/checkout@v4
2424

2525
- name: Setup Elixir
2626
uses: erlef/setup-beam@v1.16.0

.github/workflows/unit-test-reusable.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
runs-on: ${{ inputs.os }}
2121

2222
steps:
23-
- uses: actions/checkout@v3
23+
- uses: actions/checkout@v4
2424

2525
- name: Setup Elixir
2626
uses: erlef/setup-beam@v1.16.0

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## v0.2.0 (2024-01-30)
4+
5+
- Add support to use options on translate ([#26](https://github.com/hergetto/deepl_ex/pull/26))
6+
37
## v0.1.1 (2023-08-22)
48

59
- Automatically determine the tier based on the api key ([#12](https://github.com/hergetto/deepl_ex/pull/12))

lib/deepl.ex

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@ defmodule DeeplEx.DeepL do
55
def translate(client, %{
66
source_language: source_language,
77
target_language: target_language,
8-
text: text
8+
text: text,
9+
options: options
910
}) do
10-
Tesla.post(client, "/translate", %{
11-
text: [
12-
text
13-
],
14-
source_lang: parse_language(source_language),
15-
target_lang: parse_language(target_language)
16-
})
11+
content =
12+
Map.merge(options, %{
13+
text: [
14+
text
15+
],
16+
source_lang: parse_language(source_language),
17+
target_lang: parse_language(target_language)
18+
})
19+
20+
Tesla.post(client, "/translate", content)
1721
end
1822

1923
def client(api_key, tier) do

lib/deepl_ex.ex

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
6182
end

lib/helpers/options_validator.ex

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
defmodule DeeplEx.OptionsValidator do
2+
@moduledoc """
3+
This module is responsible for validating the options for translate that are supplied.
4+
"""
5+
6+
@valid_options [
7+
:split_sentences,
8+
:preserve_formatting,
9+
:formality,
10+
:glossary_id,
11+
:tag_handling,
12+
:outline_detection,
13+
:non_splitting_tags,
14+
:splitting_tags,
15+
:ignore_tags
16+
]
17+
18+
@doc false
19+
def valid_options?(options) when is_map(options) do
20+
options_keys = Map.keys(options)
21+
22+
case options_keys -- @valid_options do
23+
[] -> {:valid_options?, true}
24+
_ -> {:valid_options?, false}
25+
end
26+
end
27+
28+
@doc false
29+
def valid_options?(_), do: {:valid_options?, false}
30+
31+
@doc """
32+
Returns a list containing the valid options for translate
33+
"""
34+
def valid_options, do: @valid_options
35+
end

0 commit comments

Comments
 (0)