Skip to content

Commit

Permalink
[DeepL] Fix splitting of EN locale codes according to docs
Browse files Browse the repository at this point in the history
  • Loading branch information
cgeomar authored and zerolab committed Nov 15, 2023
1 parent f54cca6 commit a3efa8f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
10 changes: 6 additions & 4 deletions wagtail_localize/machine_translators/deepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@


def language_code(code, is_target=False):
# DeepL supports targeting Brazillian Portuguese but doesn't have this for other languages
if is_target and code in ["pt-pt", "pt-br"]:
return code
# DeepL supports targeting Brazilian Portuguese and requires to specifically request American or British English.
# @see https://www.deepl.com/en/docs-api/translate-text/translate-text
upper_code = code.upper()
if is_target and upper_code in ["PT-PT", "PT-BR", "EN-US", "EN-GB"]:
return upper_code

return code.split("-")[0].upper()
return upper_code.split("-")[0]


class DeepLTranslator(BaseMachineTranslator):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.test import TestCase, override_settings

from wagtail_localize.machine_translators import get_machine_translator
from wagtail_localize.machine_translators.deepl import DeepLTranslator
from wagtail_localize.machine_translators.deepl import DeepLTranslator, language_code


DEEPL_SETTINGS_FREE_ENDPOINT = {
Expand Down Expand Up @@ -29,3 +29,27 @@ def test_paid_api_endpoint(self):
self.assertIsInstance(translator, DeepLTranslator)
paid_api_endpoint = translator.get_api_endpoint()
self.assertEqual(paid_api_endpoint, "https://api.deepl.com/v2/translate")

def test_language_code_as_source(self):
mapping = {
"pt-pt": "PT",
"pt-br": "PT",
"en-us": "EN",
"en-gb": "EN",
"es-es": "ES",
}
for code, expected_value in mapping.items():
with self.subTest(f"Testing language_code with {code}"):
self.assertEqual(expected_value, language_code(code, is_target=False))

def test_language_code_as_target(self):
mapping = {
"pt-pt": "PT-PT",
"pt-br": "PT-BR",
"en-us": "EN-US",
"en-gb": "EN-GB",
"es-es": "ES",
}
for code, expected_value in mapping.items():
with self.subTest(f"Testing language_code with {code} as target"):
self.assertEqual(expected_value, language_code(code, is_target=True))

0 comments on commit a3efa8f

Please sign in to comment.