eric7/Plugins/UiExtensionPlugins/Translator/TranslatorEngines/DeepLEngine.py

branch
eric7
changeset 9145
520a70c5437f
parent 8881
54e42bc2437a
child 9148
b31f0d894b55
equal deleted inserted replaced
9144:135240382a3e 9145:520a70c5437f
19 class DeepLEngine(TranslationEngine): 19 class DeepLEngine(TranslationEngine):
20 """ 20 """
21 Class implementing the translation engine for the DeepL 21 Class implementing the translation engine for the DeepL
22 translation service. 22 translation service.
23 """ 23 """
24 TranslatorUrl = "https://api.deepl.com/v1/translate" 24 TranslatorUrls = {
25 "pro": "https://api.deepl.com/v2/translate",
26 "free": "https://api-free.deepl.com/v2/translate",
27 }
25 MaxTranslationTextLen = 30 * 1024 28 MaxTranslationTextLen = 30 * 1024
26 29
27 def __init__(self, plugin, parent=None): 30 def __init__(self, plugin, parent=None):
28 """ 31 """
29 Constructor 32 Constructor
51 Public method to get the supported languages. 54 Public method to get the supported languages.
52 55
53 @return list of supported language codes 56 @return list of supported language codes
54 @rtype list of str 57 @rtype list of str
55 """ 58 """
56 return ["de", "en", "es", "fr", "it", "nl", "pl", ] 59 return ["bg", "cs", "da", "de", "el", "en", "es", "et", "fi", "fr",
60 "hu", "id", "it", "ja", "lt", "lv", "nl", "pl", "pt", "ro",
61 "ru", "sk", "sl", "sv", "tr", "zh"]
57 62
58 def getTranslation(self, requestObject, text, originalLanguage, 63 def getTranslation(self, requestObject, text, originalLanguage,
59 translationLanguage): 64 translationLanguage):
60 """ 65 """
61 Public method to translate the given text. 66 Public method to translate the given text.
87 encodedText = ( 92 encodedText = (
88 QByteArray(Utilities.html_encode(text).encode("utf-8")) 93 QByteArray(Utilities.html_encode(text).encode("utf-8"))
89 .toPercentEncoding() 94 .toPercentEncoding()
90 ) 95 )
91 request = params + encodedText 96 request = params + encodedText
92 response, ok = requestObject.post(QUrl(self.TranslatorUrl), request) 97 translatorUrl = (
98 DeepLEngine.TranslatorUrls["free"]
99 if apiKey.endswith(":fx") else
100 DeepLEngine.TranslatorUrls["pro"]
101 )
102 response, ok = requestObject.post(QUrl(translatorUrl), request)
93 if ok: 103 if ok:
94 try: 104 try:
95 responseDict = json.loads(response) 105 responseDict = json.loads(response)
96 except ValueError: 106 except ValueError:
97 return self.tr("Invalid response received from DeepL"), False 107 return self.tr("Invalid response received from DeepL"), False

eric ide

mercurial