Skip to content

Commit

Permalink
Fix deepl detect source language
Browse files Browse the repository at this point in the history
  • Loading branch information
simonprev committed Mar 29, 2024
1 parent e141a3b commit 0933829
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/machine_translations/provider/deepl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ defmodule Accent.MachineTranslations.Provider.Deepl do
def translate(provider, contents, source, target) do
with {:ok, {source, target}} <-
Accent.MachineTranslations.map_source_and_target(source, target, @supported_languages),
params = %{text: contents, source_lang: String.upcase(source), target_lang: String.upcase(target)},
params = %{text: contents, source_lang: source && String.upcase(source), target_lang: String.upcase(target)},
{:ok, %{body: %{"translations" => translations}}} <-
Tesla.post(client(provider.config["key"]), "translate", params) do
{:ok, Enum.map(translations, &%TranslatedText{text: &1["text"]})}
Expand Down
47 changes: 47 additions & 0 deletions test/machine_translations/machine_translations_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,52 @@ defmodule AccentTest.MachineTranslations do
{:error, error} = MachineTranslations.translate(entries, source_language, target_language, config)
assert error === "Something"
end

test "deepl" do
mock_global(fn
%{body: body, url: "https://api-free.deepl.com/v2/translate"} ->
assert Jason.decode!(body) === %{"source_lang" => "FR", "target_lang" => "EN", "text" => ["Test"]}

%Tesla.Env{status: 200, body: %{"translations" => [%{"text" => "Translated"}]}}
end)

entries = [%Langue.Entry{value: "Test", value_type: "string", key: "."}]
source_language = "fr"
target_language = "en"
provider_config = %{"key" => "test"}
config = %{"provider" => "deepl", "config" => provider_config}

[entry] = MachineTranslations.translate(entries, source_language, target_language, config)
assert entry.value === "Translated"
end

test "deepl error" do
mock_global(fn %{url: "https://api-free.deepl.com/v2/translate"} ->
%Tesla.Env{status: 400, body: "Something"}
end)

entries = [%Langue.Entry{value: "Test", value_type: "string", key: "."}]
source_language = "fr"
target_language = "en"
provider_config = %{"key" => "test"}
config = %{"provider" => "deepl", "config" => provider_config}

{:error, error} = MachineTranslations.translate(entries, source_language, target_language, config)
assert error === "Something"
end

test "deepl detect source" do
mock_global(fn %{url: "https://api-free.deepl.com/v2/translate"} ->
%Tesla.Env{status: 400, body: "Something"}
end)

entries = [%Langue.Entry{value: "Test", value_type: "string", key: "."}]
target_language = "en"
provider_config = %{"key" => "test"}
config = %{"provider" => "deepl", "config" => provider_config}

{:error, error} = MachineTranslations.translate(entries, nil, target_language, config)
assert error === "Something"
end
end
end
3 changes: 1 addition & 2 deletions webapp/app/styles/components/project-navigation/list.scss
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@
position: relative;
gap: 10px;
left: 1px;
padding: 5px 12px 4px;
padding: 7px 12px 6px;
text-decoration: none;
font-size: 13px;
border-radius: var(--border-radius);
margin-bottom: 3px;
color: var(--text-color-normal);

&:hover,
Expand Down
2 changes: 1 addition & 1 deletion webapp/app/styles/components/translation-index.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.wrapper {
display: flex;
margin-top: 30px;
margin-top: 10px;

& > div:first-of-type {
margin-right: 40px;
Expand Down
25 changes: 9 additions & 16 deletions webapp/app/styles/components/translation-navigation.scss
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
.navigation--alt {
border-bottom: 1px solid var(--input-border-color);

.navigation-list-item {
margin-right: 6px;
}

.navigation-list-item-link {
position: relative;
top: 1px;
padding: 10px 15px 8px 10px;
border-bottom: 2px solid transparent;
padding: 2px 11px 2px 10px;
border-radius: var(--border-radius);

&:global(.active) {
border-bottom-color: var(--color-primary);
color: var(--color-primary);
background: var(--background-light-highlight);
opacity: 0.7;
}
}
}

.navigation-list {
display: flex;
align-items: stretch;
min-height: 40px;
position: relative;
left: -6px;
z-index: 10;
Expand All @@ -45,21 +40,19 @@
position: relative;
padding: 5px 5px 6px 3px;
text-decoration: none;
font-size: 14px;
font-size: 13px;
font-weight: 600;
color: var(--color-black);

&:focus,
&:hover,
:global(&.active) {
color: var(--color-primary);

.navigation-list-item-link-text {
opacity: 1;
}

.navigation-list-item-link-icon {
stroke: var(--color-primary);
stroke: currentColor;
}
}
}
Expand All @@ -73,9 +66,9 @@
.navigation-list-item-link-icon {
transition: 0.2s ease-in-out;
transition-property: stroke;
width: 16px;
height: 16px;
margin-right: 7px;
width: 12px;
height: 12px;
margin-right: 5px;
opacity: 0.6;
stroke: var(--color-black);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
@onUpdateText={{fn this.onUpdateText}}
/>
</div>
<AsyncButton @onClick={{fn this.save}} @loading={{this.isSaving}} class='button button--filled button--iconOnly'>
<AsyncButton @onClick={{fn this.save}} @loading={{this.isSaving}} class='button button--filled'>
{{t 'components.related_translations_list.save_button'}}
</AsyncButton>
{{/unless}}
Expand Down

0 comments on commit 0933829

Please sign in to comment.