Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeepL offers a free API now #604

Closed
wants to merge 9 commits into from
14 changes: 10 additions & 4 deletions wagtail_localize/machine_translators/deepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ def language_code(code, is_target=False):
return code

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


class DeepLTranslator(BaseMachineTranslator):
display_name = "DeepL"


def get_api_endpoint(self):
if self.options["AUTH_KEY"].endswith(":fx"):
return "https://api-free.deepl.com/v2/translate"
return "https://api.deepl.com/v2/translate"

def translate(self, source_locale, target_locale, strings):

response = requests.post(
"https://api.deepl.com/v2/translate",
self.get_api_endpoint(),
{
"auth_key": self.options["AUTH_KEY"],
"text": [string.data for string in strings],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

from django.test import TestCase, override_settings

from wagtail_localize.machine_translators.deepl import DeepLTranslator


class TestDeeplTranslator(TestCase):
@override_settings(WAGTAILLOCALIZE_MACHINE_TRANSLATOR["CLASS"]="wagtail_localize.machine_translators.deepl.DeepLTranslator")
def setUp(self):
pass

@override_settings(WAGTAILLOCALIZE_MACHINE_TRANSLATOR["OPTIONS"]["AUTH_KEY"]="asd-23-ssd-243-adsf-dummy-auth-key:fx")
def test_free_api_endpoint(self):
free_api_endpoint = DeepLTranslator({}).get_api_endpoint()
self.assertEqual(free_api_endpoint, "https://api-free.deepl.com/v2/translate")

@override_settings(WAGTAILLOCALIZE_MACHINE_TRANSLATOR["OPTIONS"]["AUTH_KEY"]="asd-23-ssd-243-adsf-dummy-auth-key:bla")
def test_paid_api_endpoint(self):
paid_api_endpoint = DeepLTranslator({}).get_api_endpoint()
self.assertEqual(paid_api_endpoint, "https://api.deepl.com/v2/translate")
4 changes: 4 additions & 0 deletions wagtail_localize/test/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,13 @@

WAGTAILLOCALIZE_MACHINE_TRANSLATOR = {
"CLASS": "wagtail_localize.machine_translators.dummy.DummyTranslator",
"OPTIONS": {
"AUTH_KEY": "<Your DeepL key here>",
},
}



DEFAULT_AUTO_FIELD = "django.db.models.AutoField"

if WAGTAIL_VERSION >= (2, 15):
Expand Down