|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2014 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the Google V2 translation engine. |
|
8 """ |
|
9 |
|
10 import json |
|
11 |
|
12 from PyQt5.QtCore import QUrl, QTimer |
|
13 |
|
14 from .TranslationEngine import TranslationEngine |
|
15 |
|
16 |
|
17 class GoogleV2Engine(TranslationEngine): |
|
18 """ |
|
19 Class implementing the translation engine for the new Google |
|
20 translation service. |
|
21 """ |
|
22 TranslatorUrl = "https://www.googleapis.com/language/translate/v2" |
|
23 |
|
24 def __init__(self, plugin, parent=None): |
|
25 """ |
|
26 Constructor |
|
27 |
|
28 @param plugin reference to the plugin object (TranslatorPlugin) |
|
29 @param parent reference to the parent object (QObject) |
|
30 """ |
|
31 super().__init__(plugin, parent) |
|
32 |
|
33 QTimer.singleShot(0, self.availableTranslationsLoaded.emit) |
|
34 |
|
35 def engineName(self): |
|
36 """ |
|
37 Public method to return the name of the engine. |
|
38 |
|
39 @return engine name (string) |
|
40 """ |
|
41 return "googlev2" |
|
42 |
|
43 def supportedLanguages(self): |
|
44 """ |
|
45 Public method to get the supported languages. |
|
46 |
|
47 @return list of supported language codes (list of string) |
|
48 """ |
|
49 return ["ar", "be", "bg", "bs", "ca", "cs", "da", "de", "el", "en", |
|
50 "es", "et", "fi", "fr", "ga", "gl", "hi", "hr", "hu", "id", |
|
51 "is", "it", "iw", "ja", "ka", "ko", "lt", "lv", "mk", "mt", |
|
52 "nl", "no", "pl", "pt", "ro", "ru", "sk", "sl", "sq", "sr", |
|
53 "sv", "th", "tl", "tr", "uk", "vi", "zh-CN", "zh-TW", |
|
54 ] |
|
55 |
|
56 def getTranslation(self, requestObject, text, originalLanguage, |
|
57 translationLanguage): |
|
58 """ |
|
59 Public method to translate the given text. |
|
60 |
|
61 @param requestObject reference to the request object |
|
62 (TranslatorRequest) |
|
63 @param text text to be translated (string) |
|
64 @param originalLanguage language code of the original (string) |
|
65 @param translationLanguage language code of the translation (string) |
|
66 @return tuple of translated text (string) and flag indicating |
|
67 success (boolean) |
|
68 """ |
|
69 apiKey = self.plugin.getPreferences("GoogleV2Key") |
|
70 if not apiKey: |
|
71 return self.tr("Google V2: A valid Google Translate key is" |
|
72 " required."), False |
|
73 |
|
74 params = "?key={3}&source={0}&target={1}&q={2}".format( |
|
75 originalLanguage, translationLanguage, text, apiKey) |
|
76 url = QUrl(self.TranslatorUrl + params) |
|
77 response, ok = requestObject.get(url) |
|
78 if ok: |
|
79 response = str(response, "utf-8", "replace") |
|
80 try: |
|
81 responseDict = json.loads(response) |
|
82 except ValueError: |
|
83 return self.tr("Google V2: Invalid response received"), False |
|
84 |
|
85 if ( |
|
86 "data" not in responseDict or |
|
87 "translations" not in responseDict["data"] |
|
88 ): |
|
89 return self.tr("Google V2: No translation available."), False |
|
90 |
|
91 result = "" |
|
92 translations = responseDict["data"]["translations"] |
|
93 for translation in translations: |
|
94 result += translation["translatedText"] |
|
95 if translation != translations[-1]: |
|
96 result += "<br/>" |
|
97 else: |
|
98 result = response |
|
99 return result, ok |