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
16 changes: 13 additions & 3 deletions wagtail_localize/machine_translators/deepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,25 @@ def language_code(code, is_target=False):

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

def auth_key_is_free_account(auth_key: str) -> bool:
# https://github.com/DeepLcom/deepl-python/blob/main/deepl/util.py
"""Returns True if the given authentication key belongs to a DeepL API Free
account, otherwise False."""
return auth_key.endswith(":fx")


class DeepLTranslator(BaseMachineTranslator):
display_name = "DeepL"

_DEEPL_SERVER_URL = "https://api.deepl.com/v2/translate"
_DEEPL_SERVER_URL_FREE = "https://api-free.deepl.com/v2/translate"

def translate(self, source_locale, target_locale, strings):
auth_key = self.options["AUTH_KEY"]
server_url = self._DEEPL_SERVER_URL_FREE if auth_key_is_free_account(auth_key) else self._DEEPL_SERVER_URL
response = requests.post(
"https://api.deepl.com/v2/translate",
server_url,
{
"auth_key": self.options["AUTH_KEY"],
"auth_key": auth_key,
"text": [string.data for string in strings],
"tag_handling": "xml",
"source_lang": language_code(source_locale.language_code),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

from django.test import TestCase

from wagtail_localize.machine_translators.deepl import auth_key_is_free_account


class TestDeeplTranslator(TestCase):
def setUp(self):
self.free_auth_key = "asd-23-ssd-243-adsf-dummy-auth-key:fx"
self.paid_auth_key = "asd-23-ssd-243-adsf-dummy-auth-key:bla"

def test_api_free(self):
self.assertEqual(auth_key_is_free_account(self.free_auth_key), True)

def test_api_paid(self):
self.assertEqual(auth_key_is_free_account(self.paid_auth_key), False)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a start, but I was thinking checking that DeepLTranslator.translate uses the correct URL based on the key, since that is what we want to test first and foremost

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That it is what I also wanted but I dont know how to do that; my apologies. I will learn more about writting tests and come back to the issue again.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look for mocking (e.g. https://bhch.github.io/posts/2017/09/python-testing-how-to-mock-requests-during-tests/)

alternatively, move the logic to return a URL to a method in DeepLTranslator (e.g. get_api_endpoint), and use that in requests.post. Then you can test with override_settings to change the key

in your test you could call DeepLTranslator({}).get_api_endpoint() and check its value