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

branch
eric7
changeset 9148
b31f0d894b55
parent 8881
54e42bc2437a
--- a/eric7/Plugins/UiExtensionPlugins/Translator/TranslatorEngines/GoogleV2Engine.py	Sun Jun 12 16:05:27 2022 +0200
+++ b/eric7/Plugins/UiExtensionPlugins/Translator/TranslatorEngines/GoogleV2Engine.py	Mon Jun 13 16:39:53 2022 +0200
@@ -9,24 +9,28 @@
 
 import json
 
-from PyQt6.QtCore import QUrl, QTimer
+from PyQt6.QtCore import QByteArray, QTimer, QUrl
 
 from .TranslationEngine import TranslationEngine
 
+import Utilities
+
 
 class GoogleV2Engine(TranslationEngine):
     """
     Class implementing the translation engine for the new Google
     translation service.
     """
-    TranslatorUrl = "https://www.googleapis.com/language/translate/v2"
+    TranslatorUrl = "https://translation.googleapis.com/language/translate/v2"
     
     def __init__(self, plugin, parent=None):
         """
         Constructor
         
-        @param plugin reference to the plugin object (TranslatorPlugin)
-        @param parent reference to the parent object (QObject)
+        @param plugin reference to the plugin object
+        @type TranslatorPlugin
+        @param parent reference to the parent object
+        @type QObject
         """
         super().__init__(plugin, parent)
         
@@ -36,7 +40,8 @@
         """
         Public method to return the name of the engine.
         
-        @return engine name (string)
+        @return engine name
+        @rtype str
         """
         return "googlev2"
     
@@ -44,7 +49,8 @@
         """
         Public method to get the supported languages.
         
-        @return list of supported language codes (list of string)
+        @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",
@@ -59,22 +65,30 @@
         Public method to translate the given text.
         
         @param requestObject reference to the request object
-            (TranslatorRequest)
-        @param text text to be translated (string)
-        @param originalLanguage language code of the original (string)
-        @param translationLanguage language code of the translation (string)
-        @return tuple of translated text (string) and flag indicating
-            success (boolean)
+        @type TranslatorRequest
+        @param text text to be translated
+        @type str
+        @param originalLanguage language code of the original
+        @type str
+        @param translationLanguage language code of the translation
+        @type str
+        @return tuple of translated text and flag indicating success
+        @rtype tuple of (str, bool)
         """
         apiKey = self.plugin.getPreferences("GoogleV2Key")
         if not apiKey:
             return self.tr("Google V2: A valid Google Translate key is"
                            " required."), False
         
-        params = "?key={3}&source={0}&target={1}&q={2}".format(
-            originalLanguage, translationLanguage, text, apiKey)
-        url = QUrl(self.TranslatorUrl + params)
-        response, ok = requestObject.get(url)
+        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()
+        )
+        request = params + encodedText
+        response, ok = requestObject.post(QUrl(self.TranslatorUrl), request)
         if ok:
             response = str(response, "utf-8", "replace")
             try:

eric ide

mercurial