From 321d8a64953c0b90ed0bff77d172cb6b214efbb9 Mon Sep 17 00:00:00 2001 From: Daniel Jones Date: Wed, 6 Oct 2021 16:57:51 +0200 Subject: [PATCH] Add constants for all supported language codes --- CHANGELOG.md | 3 ++- deepl/translator.py | 29 +++++++++++++++++++++++++++++ tests/test_general.py | 9 +++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93566c6..3c1308b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added -* Add `Translator.get_glossary_languages()` to query language pairs supported for glossaries. +* Add `Translator.get_glossary_languages()` to query language pairs supported for glossaries. +* Add constants for all supported languages codes, for example: `Language.GERMAN`. ### Changed * Internal language caching and client-side checking of language codes are removed. ### Deprecated diff --git a/deepl/translator.py b/deepl/translator.py index 7474766..da78459 100644 --- a/deepl/translator.py +++ b/deepl/translator.py @@ -302,6 +302,35 @@ def remove_regional_variant(language: Union[str]) -> str: """Removes the regional variant from a language, e.g. EN-US gives EN""" return str(language).upper()[0:2] + BULGARIAN = "bg" + CZECH = "cs" + DANISH = "da" + GERMAN = "de" + GREEK = "el" + ENGLISH = "en" # Only usable as a source language + ENGLISH_BRITISH = "en-GB" # Only usable as a target language + ENGLISH_AMERICAN = "en-US" # Only usable as a target language + SPANISH = "es" + ESTONIAN = "et" + FINNISH = "fi" + FRENCH = "fr" + HUNGARIAN = "hu" + ITALIAN = "it" + JAPANESE = "ja" + LITHUANIAN = "lt" + LATVIAN = "lv" + DUTCH = "nl" + POLISH = "pl" + PORTUGUESE = "pt" # Only usable as a source language + PORTUGUESE_BRAZILIAN = "pt-BR" # Only usable as a target language + PORTUGUESE_EUROPEAN = "pt-PT" # Only usable as a target language + ROMANIAN = "ro" + RUSSIAN = "ru" + SLOVAK = "sk" + SLOVENIAN = "sl" + SWEDISH = "sv" + CHINESE = "zh" + class GlossaryLanguagePair: """Information about a pair of languages supported for DeepL glossaries. diff --git a/tests/test_general.py b/tests/test_general.py index 95c5c3f..9c16885 100644 --- a/tests/test_general.py +++ b/tests/test_general.py @@ -28,6 +28,15 @@ def test_example_translation(lang, translator): assert "proton" in result_text +def test_translate_with_enums(translator): + result = translator.translate_text( + example_text["EN"], + source_lang=deepl.Language.ENGLISH, + target_lang=deepl.Language.GERMAN, + ) + assert example_text["DE"] == result.text + + def test_invalid_authkey(server): translator = deepl.Translator("invalid", server_url=server.server_url) with pytest.raises(deepl.exceptions.AuthorizationException):