diff --git a/wagtail_localize/machine_translators/deepl.py b/wagtail_localize/machine_translators/deepl.py index 3de38fade..1d6ce5766 100644 --- a/wagtail_localize/machine_translators/deepl.py +++ b/wagtail_localize/machine_translators/deepl.py @@ -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): diff --git a/wagtail_localize/machine_translators/tests/test_deepl_translator.py b/wagtail_localize/machine_translators/tests/test_deepl_translator.py index c417848b2..5cfa8b4b7 100644 --- a/wagtail_localize/machine_translators/tests/test_deepl_translator.py +++ b/wagtail_localize/machine_translators/tests/test_deepl_translator.py @@ -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 = { @@ -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))