From d36b89749f0a3512b2560323dc609c471286c8f9 Mon Sep 17 00:00:00 2001 From: Marco Bonetti Date: Thu, 11 May 2023 13:03:18 +0200 Subject: [PATCH] Deepl: use the PRO API endpoint when using a PRO API key. Fixes #278 --- CHANGES | 2 +- rosetta/translate_utils.py | 7 ++++++- .../vcr_cassettes/test_47_2_deeps_ajax_translation.yaml | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CHANGES b/CHANGES index 89bc85a..fdd8272 100644 --- a/CHANGES +++ b/CHANGES @@ -4,7 +4,7 @@ Version History Version 0.9.10 (unreleased) --------------------------- * Fix link to polib. (#277, thanks @gamboz) - +* Deepl: use the PRO API endpoint when using a PRO API key. (#278, thanks @nullcode) Version 0.9.9 diff --git a/rosetta/translate_utils.py b/rosetta/translate_utils.py index 28032c0..b4f2b52 100644 --- a/rosetta/translate_utils.py +++ b/rosetta/translate_utils.py @@ -48,8 +48,13 @@ def translate(text, from_language, to_language): def translate_by_deepl(text, to_language, auth_key): + if auth_key.lower().endswith(":fx"): + endpoint = "https://api-free.deepl.com" + else: + endpoint = "https://api.deepl.com" + r = requests.post( - "https://api-free.deepl.com/v2/translate", + f"{endpoint}/v2/translate", headers={"Authorization": f"DeepL-Auth-Key {auth_key}"}, data={ "target_lang": to_language.upper(), diff --git a/testproject/fixtures/vcr_cassettes/test_47_2_deeps_ajax_translation.yaml b/testproject/fixtures/vcr_cassettes/test_47_2_deeps_ajax_translation.yaml index c633f57..bab63a5 100644 --- a/testproject/fixtures/vcr_cassettes/test_47_2_deeps_ajax_translation.yaml +++ b/testproject/fixtures/vcr_cassettes/test_47_2_deeps_ajax_translation.yaml @@ -17,7 +17,7 @@ interactions: User-Agent: - python-requests/2.26.0 method: POST - uri: https://api-free.deepl.com/v2/translate + uri: https://api.deepl.com/v2/translate response: body: {string: '{"translations": [{"detected_source_language": "EN", "text": "Salut tout le monde"}]}'} headers: