20 class GoogleV1Engine(TranslationEngine): |
20 class GoogleV1Engine(TranslationEngine): |
21 """ |
21 """ |
22 Class implementing the translation engine for the old Google |
22 Class implementing the translation engine for the old Google |
23 translation service. |
23 translation service. |
24 """ |
24 """ |
|
25 |
25 TranslatorUrl = "https://translate.googleapis.com/translate_a/single" |
26 TranslatorUrl = "https://translate.googleapis.com/translate_a/single" |
26 TextToSpeechUrl = "https://translate.google.com/translate_tts" |
27 TextToSpeechUrl = "https://translate.google.com/translate_tts" |
27 TextToSpeechLimit = 100 |
28 TextToSpeechLimit = 100 |
28 |
29 |
29 def __init__(self, plugin, parent=None): |
30 def __init__(self, plugin, parent=None): |
30 """ |
31 """ |
31 Constructor |
32 Constructor |
32 |
33 |
33 @param plugin reference to the plugin object |
34 @param plugin reference to the plugin object |
34 @type TranslatorPlugin |
35 @type TranslatorPlugin |
35 @param parent reference to the parent object |
36 @param parent reference to the parent object |
36 @type QObject |
37 @type QObject |
37 """ |
38 """ |
38 super().__init__(plugin, parent) |
39 super().__init__(plugin, parent) |
39 |
40 |
40 QTimer.singleShot(0, self.availableTranslationsLoaded.emit) |
41 QTimer.singleShot(0, self.availableTranslationsLoaded.emit) |
41 |
42 |
42 def engineName(self): |
43 def engineName(self): |
43 """ |
44 """ |
44 Public method to return the name of the engine. |
45 Public method to return the name of the engine. |
45 |
46 |
46 @return engine name |
47 @return engine name |
47 @rtype str |
48 @rtype str |
48 """ |
49 """ |
49 return "googlev1" |
50 return "googlev1" |
50 |
51 |
51 def supportedLanguages(self): |
52 def supportedLanguages(self): |
52 """ |
53 """ |
53 Public method to get the supported languages. |
54 Public method to get the supported languages. |
54 |
55 |
55 @return list of supported language codes |
56 @return list of supported language codes |
56 @rtype list of str |
57 @rtype list of str |
57 """ |
58 """ |
58 return ["ar", "be", "bg", "bs", "ca", "cs", "da", "de", "el", "en", |
59 return [ |
59 "es", "et", "fi", "fr", "ga", "gl", "hi", "hr", "hu", "id", |
60 "ar", |
60 "is", "it", "iw", "ja", "ka", "ko", "lt", "lv", "mk", "mt", |
61 "be", |
61 "nl", "no", "pl", "pt", "ro", "ru", "sk", "sl", "sq", "sr", |
62 "bg", |
62 "sv", "th", "tl", "tr", "uk", "vi", "zh-CN", "zh-TW", |
63 "bs", |
63 ] |
64 "ca", |
64 |
65 "cs", |
|
66 "da", |
|
67 "de", |
|
68 "el", |
|
69 "en", |
|
70 "es", |
|
71 "et", |
|
72 "fi", |
|
73 "fr", |
|
74 "ga", |
|
75 "gl", |
|
76 "hi", |
|
77 "hr", |
|
78 "hu", |
|
79 "id", |
|
80 "is", |
|
81 "it", |
|
82 "iw", |
|
83 "ja", |
|
84 "ka", |
|
85 "ko", |
|
86 "lt", |
|
87 "lv", |
|
88 "mk", |
|
89 "mt", |
|
90 "nl", |
|
91 "no", |
|
92 "pl", |
|
93 "pt", |
|
94 "ro", |
|
95 "ru", |
|
96 "sk", |
|
97 "sl", |
|
98 "sq", |
|
99 "sr", |
|
100 "sv", |
|
101 "th", |
|
102 "tl", |
|
103 "tr", |
|
104 "uk", |
|
105 "vi", |
|
106 "zh-CN", |
|
107 "zh-TW", |
|
108 ] |
|
109 |
65 def hasTTS(self): |
110 def hasTTS(self): |
66 """ |
111 """ |
67 Public method indicating the Text-to-Speech capability. |
112 Public method indicating the Text-to-Speech capability. |
68 |
113 |
69 @return flag indicating the Text-to-Speech capability |
114 @return flag indicating the Text-to-Speech capability |
70 @rtype bool |
115 @rtype bool |
71 """ |
116 """ |
72 return True |
117 return True |
73 |
118 |
74 def getTranslation(self, requestObject, text, originalLanguage, |
119 def getTranslation( |
75 translationLanguage): |
120 self, requestObject, text, originalLanguage, translationLanguage |
|
121 ): |
76 """ |
122 """ |
77 Public method to translate the given text. |
123 Public method to translate the given text. |
78 |
124 |
79 @param requestObject reference to the request object |
125 @param requestObject reference to the request object |
80 @type TranslatorRequest |
126 @type TranslatorRequest |
81 @param text text to be translated |
127 @param text text to be translated |
82 @type str |
128 @type str |
83 @param originalLanguage language code of the original |
129 @param originalLanguage language code of the original |
87 @return tuple of translated text and flag indicating success |
133 @return tuple of translated text and flag indicating success |
88 @rtype tuple of (str, bool) |
134 @rtype tuple of (str, bool) |
89 """ |
135 """ |
90 params = QByteArray( |
136 params = QByteArray( |
91 "client=gtx&sl={0}&tl={1}&dt=t&dt=bd&ie=utf-8&oe=utf-8&q=".format( |
137 "client=gtx&sl={0}&tl={1}&dt=t&dt=bd&ie=utf-8&oe=utf-8&q=".format( |
92 originalLanguage, translationLanguage).encode("utf-8")) |
138 originalLanguage, translationLanguage |
93 encodedText = ( |
139 ).encode("utf-8") |
94 QByteArray(Utilities.html_encode(text).encode("utf-8")) |
|
95 .toPercentEncoding() |
|
96 ) |
140 ) |
|
141 encodedText = QByteArray( |
|
142 Utilities.html_encode(text).encode("utf-8") |
|
143 ).toPercentEncoding() |
97 request = params + encodedText |
144 request = params + encodedText |
98 response, ok = requestObject.post(QUrl(self.TranslatorUrl), request) |
145 response, ok = requestObject.post(QUrl(self.TranslatorUrl), request) |
99 if ok: |
146 if ok: |
100 try: |
147 try: |
101 # clean up the response |
148 # clean up the response |
102 response = re.sub(r',{2,}', ',', response) |
149 response = re.sub(r",{2,}", ",", response) |
103 responseDict = json.loads(response) |
150 responseDict = json.loads(response) |
104 except ValueError: |
151 except ValueError: |
105 return self.tr("Google V1: Invalid response received"), False |
152 return self.tr("Google V1: Invalid response received"), False |
106 |
153 |
107 if isinstance(responseDict, dict): |
154 if isinstance(responseDict, dict): |
108 sentences = responseDict["sentences"] |
155 sentences = responseDict["sentences"] |
109 result = "" |
156 result = "" |
110 for sentence in sentences: |
157 for sentence in sentences: |
111 result += sentence["trans"].replace("\n", "<br/>") |
158 result += sentence["trans"].replace("\n", "<br/>") |
112 |
159 |
113 if ( |
160 if ( |
114 self.plugin.getPreferences("GoogleEnableDictionary") and |
161 self.plugin.getPreferences("GoogleEnableDictionary") |
115 "dict" in responseDict |
162 and "dict" in responseDict |
116 ): |
163 ): |
117 dictionary = responseDict["dict"] |
164 dictionary = responseDict["dict"] |
118 for value in dictionary: |
165 for value in dictionary: |
119 result += "<hr/><u><b>{0}</b> - {1}</u><br/>".format( |
166 result += "<hr/><u><b>{0}</b> - {1}</u><br/>".format( |
120 text, value["pos"]) |
167 text, value["pos"] |
|
168 ) |
121 for entry in value["entry"]: |
169 for entry in value["entry"]: |
122 previous = (entry["previous_word"] + " " |
170 previous = ( |
123 if "previous_word" in entry else "") |
171 entry["previous_word"] + " " |
|
172 if "previous_word" in entry |
|
173 else "" |
|
174 ) |
124 word = entry["word"] |
175 word = entry["word"] |
125 reverse = entry["reverse_translation"] |
176 reverse = entry["reverse_translation"] |
126 result += "<br/>{0}<b>{1}</b> - {2}".format( |
177 result += "<br/>{0}<b>{1}</b> - {2}".format( |
127 previous, word, ", ".join(reverse)) |
178 previous, word, ", ".join(reverse) |
|
179 ) |
128 if value != dictionary[-1]: |
180 if value != dictionary[-1]: |
129 result += "<br/>" |
181 result += "<br/>" |
130 elif isinstance(responseDict, list): |
182 elif isinstance(responseDict, list): |
131 sentences = responseDict[0] |
183 sentences = responseDict[0] |
132 result = ( |
184 result = "".join([s[0] for s in sentences]).replace("\n", "<br/>") |
133 "".join([s[0] for s in sentences]).replace("\n", "<br/>") |
|
134 ) |
|
135 if ( |
185 if ( |
136 self.plugin.getPreferences("GoogleEnableDictionary") and |
186 self.plugin.getPreferences("GoogleEnableDictionary") |
137 len(responseDict) > 2 |
187 and len(responseDict) > 2 |
138 ): |
188 ): |
139 if not responseDict[1]: |
189 if not responseDict[1]: |
140 result = self.tr("Google V1: No translation found.") |
190 result = self.tr("Google V1: No translation found.") |
141 ok = False |
191 ok = False |
142 else: |
192 else: |
143 for wordTypeList in responseDict[1]: |
193 for wordTypeList in responseDict[1]: |
144 result += "<hr/><u><b>{0}</b> - {1}</u>".format( |
194 result += "<hr/><u><b>{0}</b> - {1}</u>".format( |
145 wordTypeList[0], wordTypeList[-2]) |
195 wordTypeList[0], wordTypeList[-2] |
|
196 ) |
146 for wordsList in wordTypeList[2]: |
197 for wordsList in wordTypeList[2]: |
147 reverse = wordsList[0] |
198 reverse = wordsList[0] |
148 words = wordsList[1] |
199 words = wordsList[1] |
149 result += "<br/><b>{0}</b> - {1}".format( |
200 result += "<br/><b>{0}</b> - {1}".format( |
150 reverse, ", ".join(words)) |
201 reverse, ", ".join(words) |
|
202 ) |
151 else: |
203 else: |
152 result = responseDict |
204 result = responseDict |
153 else: |
205 else: |
154 result = response |
206 result = response |
155 return result, ok |
207 return result, ok |
156 |
208 |
157 def getTextToSpeechData(self, requestObject, text, language): |
209 def getTextToSpeechData(self, requestObject, text, language): |
158 """ |
210 """ |
159 Public method to pronounce the given text. |
211 Public method to pronounce the given text. |
160 |
212 |
161 @param requestObject reference to the request object |
213 @param requestObject reference to the request object |
162 @type TranslatorRequest |
214 @type TranslatorRequest |
163 @param text text to be pronounced |
215 @param text text to be pronounced |
164 @type str |
216 @type str |
165 @param language language code of the text |
217 @param language language code of the text |