--- a/src/eric7/Plugins/UiExtensionPlugins/Translator/TranslatorEngines/GoogleV2Engine.py Wed Jul 13 11:16:20 2022 +0200 +++ b/src/eric7/Plugins/UiExtensionPlugins/Translator/TranslatorEngines/GoogleV2Engine.py Wed Jul 13 14:55:47 2022 +0200 @@ -21,49 +21,95 @@ Class implementing the translation engine for the new Google translation service. """ + TranslatorUrl = "https://translation.googleapis.com/language/translate/v2" - + def __init__(self, plugin, parent=None): """ Constructor - + @param plugin reference to the plugin object @type TranslatorPlugin @param parent reference to the parent object @type QObject """ super().__init__(plugin, parent) - + QTimer.singleShot(0, self.availableTranslationsLoaded.emit) - + def engineName(self): """ Public method to return the name of the engine. - + @return engine name @rtype str """ return "googlev2" - + def supportedLanguages(self): """ Public method to get the supported languages. - + @return list of supported language codes @rtype list of str """ - return ["ar", "be", "bg", "bs", "ca", "cs", "da", "de", "el", "en", - "es", "et", "fi", "fr", "ga", "gl", "hi", "hr", "hu", "id", - "is", "it", "iw", "ja", "ka", "ko", "lt", "lv", "mk", "mt", - "nl", "no", "pl", "pt", "ro", "ru", "sk", "sl", "sq", "sr", - "sv", "th", "tl", "tr", "uk", "vi", "zh-CN", "zh-TW", - ] - - def getTranslation(self, requestObject, text, originalLanguage, - translationLanguage): + return [ + "ar", + "be", + "bg", + "bs", + "ca", + "cs", + "da", + "de", + "el", + "en", + "es", + "et", + "fi", + "fr", + "ga", + "gl", + "hi", + "hr", + "hu", + "id", + "is", + "it", + "iw", + "ja", + "ka", + "ko", + "lt", + "lv", + "mk", + "mt", + "nl", + "no", + "pl", + "pt", + "ro", + "ru", + "sk", + "sl", + "sq", + "sr", + "sv", + "th", + "tl", + "tr", + "uk", + "vi", + "zh-CN", + "zh-TW", + ] + + def getTranslation( + self, requestObject, text, originalLanguage, translationLanguage + ): """ Public method to translate the given text. - + @param requestObject reference to the request object @type TranslatorRequest @param text text to be translated @@ -77,16 +123,19 @@ """ apiKey = self.plugin.getPreferences("GoogleV2Key") if not apiKey: - return self.tr("Google V2: A valid Google Translate key is" - " required."), False - + return ( + self.tr("Google V2: A valid Google Translate key is" " required."), + False, + ) + params = QByteArray( "key={2}&source={0}&target={1}&format=text&q=".format( - originalLanguage, translationLanguage, apiKey).encode("utf-8")) - encodedText = ( - QByteArray(Utilities.html_encode(text).encode("utf-8")) - .toPercentEncoding() + originalLanguage, translationLanguage, apiKey + ).encode("utf-8") ) + encodedText = QByteArray( + Utilities.html_encode(text).encode("utf-8") + ).toPercentEncoding() request = params + encodedText response, ok = requestObject.post(QUrl(self.TranslatorUrl), request) if ok: @@ -95,13 +144,10 @@ responseDict = json.loads(response) except ValueError: return self.tr("Google V2: Invalid response received"), False - - if ( - "data" not in responseDict or - "translations" not in responseDict["data"] - ): + + if "data" not in responseDict or "translations" not in responseDict["data"]: return self.tr("Google V2: No translation available."), False - + result = "" translations = responseDict["data"]["translations"] for translation in translations: