19 class YandexEngine(TranslationEngine): |
19 class YandexEngine(TranslationEngine): |
20 """ |
20 """ |
21 Class implementing the translation engine for the Yandex |
21 Class implementing the translation engine for the Yandex |
22 translation service. |
22 translation service. |
23 """ |
23 """ |
|
24 |
24 TranslatorUrl = "https://translate.yandex.net/api/v1.5/tr.json/translate" |
25 TranslatorUrl = "https://translate.yandex.net/api/v1.5/tr.json/translate" |
25 TranslatorLimit = 10000 |
26 TranslatorLimit = 10000 |
26 |
27 |
27 def __init__(self, plugin, parent=None): |
28 def __init__(self, plugin, parent=None): |
28 """ |
29 """ |
29 Constructor |
30 Constructor |
30 |
31 |
31 @param plugin reference to the plugin object |
32 @param plugin reference to the plugin object |
32 @type TranslatorPlugin |
33 @type TranslatorPlugin |
33 @param parent reference to the parent object |
34 @param parent reference to the parent object |
34 @type QObject |
35 @type QObject |
35 """ |
36 """ |
36 super().__init__(plugin, parent) |
37 super().__init__(plugin, parent) |
37 |
38 |
38 self.__errors = { |
39 self.__errors = { |
39 401: self.tr("Yandex: Invalid API key."), |
40 401: self.tr("Yandex: Invalid API key."), |
40 402: self.tr("Yandex: API key has been blocked."), |
41 402: self.tr("Yandex: API key has been blocked."), |
41 403: self.tr("Yandex: Daily limit for requests has been reached."), |
42 403: self.tr("Yandex: Daily limit for requests has been reached."), |
42 404: self.tr("Yandex: Daily limit for the volume of translated" |
43 404: self.tr( |
43 " text reached."), |
44 "Yandex: Daily limit for the volume of translated" " text reached." |
|
45 ), |
44 413: self.tr("Yandex: Text size exceeds the maximum."), |
46 413: self.tr("Yandex: Text size exceeds the maximum."), |
45 422: self.tr("Yandex: Text could not be translated."), |
47 422: self.tr("Yandex: Text could not be translated."), |
46 501: self.tr("Yandex: The specified translation direction is not" |
48 501: self.tr( |
47 " supported."), |
49 "Yandex: The specified translation direction is not" " supported." |
|
50 ), |
48 } |
51 } |
49 |
52 |
50 QTimer.singleShot(0, self.availableTranslationsLoaded.emit) |
53 QTimer.singleShot(0, self.availableTranslationsLoaded.emit) |
51 |
54 |
52 def engineName(self): |
55 def engineName(self): |
53 """ |
56 """ |
54 Public method to return the name of the engine. |
57 Public method to return the name of the engine. |
55 |
58 |
56 @return engine name |
59 @return engine name |
57 @rtype str |
60 @rtype str |
58 """ |
61 """ |
59 return "yandex" |
62 return "yandex" |
60 |
63 |
61 def supportedLanguages(self): |
64 def supportedLanguages(self): |
62 """ |
65 """ |
63 Public method to get the supported languages. |
66 Public method to get the supported languages. |
64 |
67 |
65 @return list of supported language codes |
68 @return list of supported language codes |
66 @rtype list of str |
69 @rtype list of str |
67 """ |
70 """ |
68 return ["ar", "be", "bg", "bs", "ca", "cs", "da", "de", "el", "en", |
71 return [ |
69 "es", "et", "fi", "fr", "ga", "gl", "hi", "hr", "hu", "id", |
72 "ar", |
70 "is", "it", "iw", "ja", "ka", "ko", "lt", "lv", "mk", "mt", |
73 "be", |
71 "nl", "no", "pl", "pt", "ro", "ru", "sk", "sl", "sq", "sr", |
74 "bg", |
72 "sv", "th", "tl", "tr", "uk", "vi", "zh-CN", "zh-TW", |
75 "bs", |
73 ] |
76 "ca", |
74 |
77 "cs", |
75 def getTranslation(self, requestObject, text, originalLanguage, |
78 "da", |
76 translationLanguage): |
79 "de", |
|
80 "el", |
|
81 "en", |
|
82 "es", |
|
83 "et", |
|
84 "fi", |
|
85 "fr", |
|
86 "ga", |
|
87 "gl", |
|
88 "hi", |
|
89 "hr", |
|
90 "hu", |
|
91 "id", |
|
92 "is", |
|
93 "it", |
|
94 "iw", |
|
95 "ja", |
|
96 "ka", |
|
97 "ko", |
|
98 "lt", |
|
99 "lv", |
|
100 "mk", |
|
101 "mt", |
|
102 "nl", |
|
103 "no", |
|
104 "pl", |
|
105 "pt", |
|
106 "ro", |
|
107 "ru", |
|
108 "sk", |
|
109 "sl", |
|
110 "sq", |
|
111 "sr", |
|
112 "sv", |
|
113 "th", |
|
114 "tl", |
|
115 "tr", |
|
116 "uk", |
|
117 "vi", |
|
118 "zh-CN", |
|
119 "zh-TW", |
|
120 ] |
|
121 |
|
122 def getTranslation( |
|
123 self, requestObject, text, originalLanguage, translationLanguage |
|
124 ): |
77 """ |
125 """ |
78 Public method to translate the given text. |
126 Public method to translate the given text. |
79 |
127 |
80 @param requestObject reference to the request object |
128 @param requestObject reference to the request object |
81 @type TranslatorRequest |
129 @type TranslatorRequest |
82 @param text text to be translated |
130 @param text text to be translated |
83 @type str |
131 @type str |
84 @param originalLanguage language code of the original |
132 @param originalLanguage language code of the original |
88 @return tuple of translated text and flag indicating success |
136 @return tuple of translated text and flag indicating success |
89 @rtype tuple of (str, bool) |
137 @rtype tuple of (str, bool) |
90 """ |
138 """ |
91 if len(text) > self.TranslatorLimit: |
139 if len(text) > self.TranslatorLimit: |
92 return ( |
140 return ( |
93 self.tr("Yandex: Only texts up to {0} characters are allowed.") |
141 self.tr("Yandex: Only texts up to {0} characters are allowed.").format( |
94 .format(self.TranslatorLimit), |
142 self.TranslatorLimit |
95 False |
143 ), |
|
144 False, |
96 ) |
145 ) |
97 |
146 |
98 apiKey = self.plugin.getPreferences("YandexKey") |
147 apiKey = self.plugin.getPreferences("YandexKey") |
99 if not apiKey: |
148 if not apiKey: |
100 return self.tr("Yandex: A valid key is required."), False |
149 return self.tr("Yandex: A valid key is required."), False |
101 |
150 |
102 params = QByteArray( |
151 params = QByteArray( |
103 "key={0}&lang={1}-{2}&text=".format( |
152 "key={0}&lang={1}-{2}&text=".format( |
104 apiKey, originalLanguage, translationLanguage).encode("utf-8")) |
153 apiKey, originalLanguage, translationLanguage |
105 encodedText = ( |
154 ).encode("utf-8") |
106 QByteArray(Utilities.html_encode(text).encode("utf-8")) |
|
107 .toPercentEncoding() |
|
108 ) |
155 ) |
|
156 encodedText = QByteArray( |
|
157 Utilities.html_encode(text).encode("utf-8") |
|
158 ).toPercentEncoding() |
109 request = params + encodedText |
159 request = params + encodedText |
110 response, ok = requestObject.post(QUrl(self.TranslatorUrl), request) |
160 response, ok = requestObject.post(QUrl(self.TranslatorUrl), request) |
111 if ok: |
161 if ok: |
112 try: |
162 try: |
113 responseDict = json.loads(response) |
163 responseDict = json.loads(response) |
114 except ValueError: |
164 except ValueError: |
115 return self.tr("Yandex: Invalid response received"), False |
165 return self.tr("Yandex: Invalid response received"), False |
116 |
166 |
117 if responseDict["code"] != 200: |
167 if responseDict["code"] != 200: |
118 try: |
168 try: |
119 error = self.__errors[responseDict["code"]] |
169 error = self.__errors[responseDict["code"]] |
120 except KeyError: |
170 except KeyError: |
121 error = self.tr( |
171 error = self.tr( |
122 "Yandex: Unknown error code ({0}) received." |
172 "Yandex: Unknown error code ({0}) received." |
123 ).format(responseDict["code"]) |
173 ).format(responseDict["code"]) |
124 return error, False |
174 return error, False |
125 |
175 |
126 sentences = responseDict["text"] |
176 sentences = responseDict["text"] |
127 result = "" |
177 result = "" |
128 for sentence in sentences: |
178 for sentence in sentences: |
129 result += sentence.replace("\n", "<br/>") |
179 result += sentence.replace("\n", "<br/>") |
130 else: |
180 else: |