|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2014 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the Yandex translation engine. |
|
8 """ |
|
9 |
|
10 import json |
|
11 |
|
12 from PyQt5.QtCore import QUrl, QByteArray, QTimer |
|
13 |
|
14 import Utilities |
|
15 |
|
16 from .TranslationEngine import TranslationEngine |
|
17 |
|
18 |
|
19 class YandexEngine(TranslationEngine): |
|
20 """ |
|
21 Class implementing the translation engine for the Yandex |
|
22 translation service. |
|
23 """ |
|
24 TranslatorUrl = "https://translate.yandex.net/api/v1.5/tr.json/translate" |
|
25 TranslatorLimit = 10000 |
|
26 |
|
27 def __init__(self, plugin, parent=None): |
|
28 """ |
|
29 Constructor |
|
30 |
|
31 @param plugin reference to the plugin object (TranslatorPlugin) |
|
32 @param parent reference to the parent object (QObject) |
|
33 """ |
|
34 super().__init__(plugin, parent) |
|
35 |
|
36 self.__errors = { |
|
37 401: self.tr("Yandex: Invalid API key."), |
|
38 402: self.tr("Yandex: API key has been blocked."), |
|
39 403: self.tr("Yandex: Daily limit for requests has been reached."), |
|
40 404: self.tr("Yandex: Daily limit for the volume of translated" |
|
41 " text reached."), |
|
42 413: self.tr("Yandex: Text size exceeds the maximum."), |
|
43 422: self.tr("Yandex: Text could not be translated."), |
|
44 501: self.tr("Yandex: The specified translation direction is not" |
|
45 " supported."), |
|
46 } |
|
47 |
|
48 QTimer.singleShot(0, self.availableTranslationsLoaded.emit) |
|
49 |
|
50 def engineName(self): |
|
51 """ |
|
52 Public method to return the name of the engine. |
|
53 |
|
54 @return engine name (string) |
|
55 """ |
|
56 return "yandex" |
|
57 |
|
58 def supportedLanguages(self): |
|
59 """ |
|
60 Public method to get the supported languages. |
|
61 |
|
62 @return list of supported language codes (list of string) |
|
63 """ |
|
64 return ["ar", "be", "bg", "bs", "ca", "cs", "da", "de", "el", "en", |
|
65 "es", "et", "fi", "fr", "ga", "gl", "hi", "hr", "hu", "id", |
|
66 "is", "it", "iw", "ja", "ka", "ko", "lt", "lv", "mk", "mt", |
|
67 "nl", "no", "pl", "pt", "ro", "ru", "sk", "sl", "sq", "sr", |
|
68 "sv", "th", "tl", "tr", "uk", "vi", "zh-CN", "zh-TW", |
|
69 ] |
|
70 |
|
71 def getTranslation(self, requestObject, text, originalLanguage, |
|
72 translationLanguage): |
|
73 """ |
|
74 Public method to translate the given text. |
|
75 |
|
76 @param requestObject reference to the request object |
|
77 (TranslatorRequest) |
|
78 @param text text to be translated (string) |
|
79 @param originalLanguage language code of the original (string) |
|
80 @param translationLanguage language code of the translation (string) |
|
81 @return tuple of translated text (string) and flag indicating |
|
82 success (boolean) |
|
83 """ |
|
84 if len(text) > self.TranslatorLimit: |
|
85 return ( |
|
86 self.tr("Yandex: Only texts up to {0} characters are allowed.") |
|
87 .format(self.TranslatorLimit), |
|
88 False |
|
89 ) |
|
90 |
|
91 apiKey = self.plugin.getPreferences("YandexKey") |
|
92 if not apiKey: |
|
93 return self.tr("Yandex: A valid key is required."), False |
|
94 |
|
95 params = QByteArray( |
|
96 "key={0}&lang={1}-{2}&text=".format( |
|
97 apiKey, originalLanguage, translationLanguage).encode("utf-8")) |
|
98 encodedText = ( |
|
99 QByteArray(Utilities.html_encode(text).encode("utf-8")) |
|
100 .toPercentEncoding() |
|
101 ) |
|
102 request = params + encodedText |
|
103 response, ok = requestObject.post(QUrl(self.TranslatorUrl), request) |
|
104 if ok: |
|
105 try: |
|
106 responseDict = json.loads(response) |
|
107 except ValueError: |
|
108 return self.tr("Yandex: Invalid response received"), False |
|
109 |
|
110 if responseDict["code"] != 200: |
|
111 try: |
|
112 error = self.__errors[responseDict["code"]] |
|
113 except KeyError: |
|
114 error = self.tr( |
|
115 "Yandex: Unknown error code ({0}) received." |
|
116 ).format(responseDict["code"]) |
|
117 return error, False |
|
118 |
|
119 sentences = responseDict["text"] |
|
120 result = "" |
|
121 for sentence in sentences: |
|
122 result += sentence.replace("\n", "<br/>") |
|
123 else: |
|
124 result = response |
|
125 return result, ok |