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

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9413
80c06d472826
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
19 class GoogleV2Engine(TranslationEngine): 19 class GoogleV2Engine(TranslationEngine):
20 """ 20 """
21 Class implementing the translation engine for the new Google 21 Class implementing the translation engine for the new Google
22 translation service. 22 translation service.
23 """ 23 """
24
24 TranslatorUrl = "https://translation.googleapis.com/language/translate/v2" 25 TranslatorUrl = "https://translation.googleapis.com/language/translate/v2"
25 26
26 def __init__(self, plugin, parent=None): 27 def __init__(self, plugin, parent=None):
27 """ 28 """
28 Constructor 29 Constructor
29 30
30 @param plugin reference to the plugin object 31 @param plugin reference to the plugin object
31 @type TranslatorPlugin 32 @type TranslatorPlugin
32 @param parent reference to the parent object 33 @param parent reference to the parent object
33 @type QObject 34 @type QObject
34 """ 35 """
35 super().__init__(plugin, parent) 36 super().__init__(plugin, parent)
36 37
37 QTimer.singleShot(0, self.availableTranslationsLoaded.emit) 38 QTimer.singleShot(0, self.availableTranslationsLoaded.emit)
38 39
39 def engineName(self): 40 def engineName(self):
40 """ 41 """
41 Public method to return the name of the engine. 42 Public method to return the name of the engine.
42 43
43 @return engine name 44 @return engine name
44 @rtype str 45 @rtype str
45 """ 46 """
46 return "googlev2" 47 return "googlev2"
47 48
48 def supportedLanguages(self): 49 def supportedLanguages(self):
49 """ 50 """
50 Public method to get the supported languages. 51 Public method to get the supported languages.
51 52
52 @return list of supported language codes 53 @return list of supported language codes
53 @rtype list of str 54 @rtype list of str
54 """ 55 """
55 return ["ar", "be", "bg", "bs", "ca", "cs", "da", "de", "el", "en", 56 return [
56 "es", "et", "fi", "fr", "ga", "gl", "hi", "hr", "hu", "id", 57 "ar",
57 "is", "it", "iw", "ja", "ka", "ko", "lt", "lv", "mk", "mt", 58 "be",
58 "nl", "no", "pl", "pt", "ro", "ru", "sk", "sl", "sq", "sr", 59 "bg",
59 "sv", "th", "tl", "tr", "uk", "vi", "zh-CN", "zh-TW", 60 "bs",
60 ] 61 "ca",
61 62 "cs",
62 def getTranslation(self, requestObject, text, originalLanguage, 63 "da",
63 translationLanguage): 64 "de",
65 "el",
66 "en",
67 "es",
68 "et",
69 "fi",
70 "fr",
71 "ga",
72 "gl",
73 "hi",
74 "hr",
75 "hu",
76 "id",
77 "is",
78 "it",
79 "iw",
80 "ja",
81 "ka",
82 "ko",
83 "lt",
84 "lv",
85 "mk",
86 "mt",
87 "nl",
88 "no",
89 "pl",
90 "pt",
91 "ro",
92 "ru",
93 "sk",
94 "sl",
95 "sq",
96 "sr",
97 "sv",
98 "th",
99 "tl",
100 "tr",
101 "uk",
102 "vi",
103 "zh-CN",
104 "zh-TW",
105 ]
106
107 def getTranslation(
108 self, requestObject, text, originalLanguage, translationLanguage
109 ):
64 """ 110 """
65 Public method to translate the given text. 111 Public method to translate the given text.
66 112
67 @param requestObject reference to the request object 113 @param requestObject reference to the request object
68 @type TranslatorRequest 114 @type TranslatorRequest
69 @param text text to be translated 115 @param text text to be translated
70 @type str 116 @type str
71 @param originalLanguage language code of the original 117 @param originalLanguage language code of the original
75 @return tuple of translated text and flag indicating success 121 @return tuple of translated text and flag indicating success
76 @rtype tuple of (str, bool) 122 @rtype tuple of (str, bool)
77 """ 123 """
78 apiKey = self.plugin.getPreferences("GoogleV2Key") 124 apiKey = self.plugin.getPreferences("GoogleV2Key")
79 if not apiKey: 125 if not apiKey:
80 return self.tr("Google V2: A valid Google Translate key is" 126 return (
81 " required."), False 127 self.tr("Google V2: A valid Google Translate key is" " required."),
82 128 False,
129 )
130
83 params = QByteArray( 131 params = QByteArray(
84 "key={2}&source={0}&target={1}&format=text&q=".format( 132 "key={2}&source={0}&target={1}&format=text&q=".format(
85 originalLanguage, translationLanguage, apiKey).encode("utf-8")) 133 originalLanguage, translationLanguage, apiKey
86 encodedText = ( 134 ).encode("utf-8")
87 QByteArray(Utilities.html_encode(text).encode("utf-8"))
88 .toPercentEncoding()
89 ) 135 )
136 encodedText = QByteArray(
137 Utilities.html_encode(text).encode("utf-8")
138 ).toPercentEncoding()
90 request = params + encodedText 139 request = params + encodedText
91 response, ok = requestObject.post(QUrl(self.TranslatorUrl), request) 140 response, ok = requestObject.post(QUrl(self.TranslatorUrl), request)
92 if ok: 141 if ok:
93 response = str(response, "utf-8", "replace") 142 response = str(response, "utf-8", "replace")
94 try: 143 try:
95 responseDict = json.loads(response) 144 responseDict = json.loads(response)
96 except ValueError: 145 except ValueError:
97 return self.tr("Google V2: Invalid response received"), False 146 return self.tr("Google V2: Invalid response received"), False
98 147
99 if ( 148 if "data" not in responseDict or "translations" not in responseDict["data"]:
100 "data" not in responseDict or
101 "translations" not in responseDict["data"]
102 ):
103 return self.tr("Google V2: No translation available."), False 149 return self.tr("Google V2: No translation available."), False
104 150
105 result = "" 151 result = ""
106 translations = responseDict["data"]["translations"] 152 translations = responseDict["data"]["translations"]
107 for translation in translations: 153 for translation in translations:
108 result += translation["translatedText"] 154 result += translation["translatedText"]
109 if translation != translations[-1]: 155 if translation != translations[-1]:

eric ide

mercurial