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

branch
eric7
changeset 9148
b31f0d894b55
parent 8881
54e42bc2437a
equal deleted inserted replaced
9147:bbf3af40c223 9148:b31f0d894b55
7 Module implementing the Google V2 translation engine. 7 Module implementing the Google V2 translation engine.
8 """ 8 """
9 9
10 import json 10 import json
11 11
12 from PyQt6.QtCore import QUrl, QTimer 12 from PyQt6.QtCore import QByteArray, QTimer, QUrl
13 13
14 from .TranslationEngine import TranslationEngine 14 from .TranslationEngine import TranslationEngine
15
16 import Utilities
15 17
16 18
17 class GoogleV2Engine(TranslationEngine): 19 class GoogleV2Engine(TranslationEngine):
18 """ 20 """
19 Class implementing the translation engine for the new Google 21 Class implementing the translation engine for the new Google
20 translation service. 22 translation service.
21 """ 23 """
22 TranslatorUrl = "https://www.googleapis.com/language/translate/v2" 24 TranslatorUrl = "https://translation.googleapis.com/language/translate/v2"
23 25
24 def __init__(self, plugin, parent=None): 26 def __init__(self, plugin, parent=None):
25 """ 27 """
26 Constructor 28 Constructor
27 29
28 @param plugin reference to the plugin object (TranslatorPlugin) 30 @param plugin reference to the plugin object
29 @param parent reference to the parent object (QObject) 31 @type TranslatorPlugin
32 @param parent reference to the parent object
33 @type QObject
30 """ 34 """
31 super().__init__(plugin, parent) 35 super().__init__(plugin, parent)
32 36
33 QTimer.singleShot(0, self.availableTranslationsLoaded.emit) 37 QTimer.singleShot(0, self.availableTranslationsLoaded.emit)
34 38
35 def engineName(self): 39 def engineName(self):
36 """ 40 """
37 Public method to return the name of the engine. 41 Public method to return the name of the engine.
38 42
39 @return engine name (string) 43 @return engine name
44 @rtype str
40 """ 45 """
41 return "googlev2" 46 return "googlev2"
42 47
43 def supportedLanguages(self): 48 def supportedLanguages(self):
44 """ 49 """
45 Public method to get the supported languages. 50 Public method to get the supported languages.
46 51
47 @return list of supported language codes (list of string) 52 @return list of supported language codes
53 @rtype list of str
48 """ 54 """
49 return ["ar", "be", "bg", "bs", "ca", "cs", "da", "de", "el", "en", 55 return ["ar", "be", "bg", "bs", "ca", "cs", "da", "de", "el", "en",
50 "es", "et", "fi", "fr", "ga", "gl", "hi", "hr", "hu", "id", 56 "es", "et", "fi", "fr", "ga", "gl", "hi", "hr", "hu", "id",
51 "is", "it", "iw", "ja", "ka", "ko", "lt", "lv", "mk", "mt", 57 "is", "it", "iw", "ja", "ka", "ko", "lt", "lv", "mk", "mt",
52 "nl", "no", "pl", "pt", "ro", "ru", "sk", "sl", "sq", "sr", 58 "nl", "no", "pl", "pt", "ro", "ru", "sk", "sl", "sq", "sr",
57 translationLanguage): 63 translationLanguage):
58 """ 64 """
59 Public method to translate the given text. 65 Public method to translate the given text.
60 66
61 @param requestObject reference to the request object 67 @param requestObject reference to the request object
62 (TranslatorRequest) 68 @type TranslatorRequest
63 @param text text to be translated (string) 69 @param text text to be translated
64 @param originalLanguage language code of the original (string) 70 @type str
65 @param translationLanguage language code of the translation (string) 71 @param originalLanguage language code of the original
66 @return tuple of translated text (string) and flag indicating 72 @type str
67 success (boolean) 73 @param translationLanguage language code of the translation
74 @type str
75 @return tuple of translated text and flag indicating success
76 @rtype tuple of (str, bool)
68 """ 77 """
69 apiKey = self.plugin.getPreferences("GoogleV2Key") 78 apiKey = self.plugin.getPreferences("GoogleV2Key")
70 if not apiKey: 79 if not apiKey:
71 return self.tr("Google V2: A valid Google Translate key is" 80 return self.tr("Google V2: A valid Google Translate key is"
72 " required."), False 81 " required."), False
73 82
74 params = "?key={3}&source={0}&target={1}&q={2}".format( 83 params = QByteArray(
75 originalLanguage, translationLanguage, text, apiKey) 84 "key={2}&source={0}&target={1}&format=text&q=".format(
76 url = QUrl(self.TranslatorUrl + params) 85 originalLanguage, translationLanguage, apiKey).encode("utf-8"))
77 response, ok = requestObject.get(url) 86 encodedText = (
87 QByteArray(Utilities.html_encode(text).encode("utf-8"))
88 .toPercentEncoding()
89 )
90 request = params + encodedText
91 response, ok = requestObject.post(QUrl(self.TranslatorUrl), request)
78 if ok: 92 if ok:
79 response = str(response, "utf-8", "replace") 93 response = str(response, "utf-8", "replace")
80 try: 94 try:
81 responseDict = json.loads(response) 95 responseDict = json.loads(response)
82 except ValueError: 96 except ValueError:

eric ide

mercurial