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

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9473
3f23dbf37dbe
--- a/src/eric7/Plugins/UiExtensionPlugins/Translator/TranslatorEngines/MicrosoftEngine.py	Wed Jul 13 11:16:20 2022 +0200
+++ b/src/eric7/Plugins/UiExtensionPlugins/Translator/TranslatorEngines/MicrosoftEngine.py	Wed Jul 13 14:55:47 2022 +0200
@@ -19,56 +19,87 @@
     Class implementing the translation engine for the Microsoft
     translation service.
     """
+
     TranslatorUrl = (
-        "https://api.cognitive.microsofttranslator.com/translate"
-        "?api-version=3.0"
+        "https://api.cognitive.microsofttranslator.com/translate" "?api-version=3.0"
     )
-    
+
     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)
-        
+
         self.__mappings = {
             "zh-CN": "zh-CHS",
             "zh-TW": "zh-CHT",
         }
-        
+
         QTimer.singleShot(0, self.availableTranslationsLoaded.emit)
-    
+
     def engineName(self):
         """
         Public method to return the name of the engine.
-        
+
         @return engine name
         @rtype str
         """
         return "microsoft"
-    
+
     def supportedLanguages(self):
         """
         Public method to get the supported languages.
-        
+
         @return list of supported language codes
         @rtype list of str
         """
-        return ["ar", "bg", "ca", "cs", "da", "de", "en",
-                "es", "et", "fi", "fr", "hi", "hu", "id",
-                "it", "ja", "ko", "lt", "lv", "mt",
-                "nl", "no", "pl", "pt", "ro", "ru", "sk", "sl",
-                "sv", "th", "tr", "uk", "vi", "zh-CN", "zh-TW",
-                ]
-    
+        return [
+            "ar",
+            "bg",
+            "ca",
+            "cs",
+            "da",
+            "de",
+            "en",
+            "es",
+            "et",
+            "fi",
+            "fr",
+            "hi",
+            "hu",
+            "id",
+            "it",
+            "ja",
+            "ko",
+            "lt",
+            "lv",
+            "mt",
+            "nl",
+            "no",
+            "pl",
+            "pt",
+            "ro",
+            "ru",
+            "sk",
+            "sl",
+            "sv",
+            "th",
+            "tr",
+            "uk",
+            "vi",
+            "zh-CN",
+            "zh-TW",
+        ]
+
     def __mapLanguageCode(self, code):
         """
         Private method to map a language code to the Microsoft code.
-        
+
         @param code language code
         @type str
         @return mapped language code
@@ -78,11 +109,11 @@
             return self.__mapping[code]
         else:
             return code
-    
+
     def __getClientDataAzure(self):
         """
         Private method to retrieve the client data.
-        
+
         @return tuple giving the API subscription key, the API subscription
             region and a flag indicating validity
         @rtype tuple of (str, str, bool)
@@ -91,12 +122,13 @@
         subscriptionRegion = self.plugin.getPreferences("MsTranslatorRegion")
         valid = bool(subscriptionKey) and bool(subscriptionRegion)
         return subscriptionKey, subscriptionRegion, valid
-    
-    def getTranslation(self, requestObject, text, originalLanguage,
-                       translationLanguage):
+
+    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
@@ -108,27 +140,28 @@
         @return tuple of translated text and flag indicating success
         @rtype tuple of (str, bool)
         """
-        subscriptionKey, subscriptionRegion, valid = (
-            self.__getClientDataAzure()
-        )
+        subscriptionKey, subscriptionRegion, valid = self.__getClientDataAzure()
         if not valid:
-            return (self.tr("""You have not registered for the Microsoft"""
-                            """ Azure Translation service."""),
-                    False)
-        
+            return (
+                self.tr(
+                    """You have not registered for the Microsoft"""
+                    """ Azure Translation service."""
+                ),
+                False,
+            )
+
         params = "&from={0}&to={1}".format(
             self.__mapLanguageCode(originalLanguage),
             self.__mapLanguageCode(translationLanguage),
         )
         url = QUrl(self.TranslatorUrl + params)
-        
+
         requestList = [{"Text": text}]
         request = QByteArray(json.dumps(requestList).encode("utf-8"))
-        
+
         headers = [
             (b"Ocp-Apim-Subscription-Key", subscriptionKey.encode("utf8")),
-            (b"Ocp-Apim-Subscription-Region",
-             subscriptionRegion.encode("utf8")),
+            (b"Ocp-Apim-Subscription-Region", subscriptionRegion.encode("utf8")),
             (b"Content-Type", b"application/json; charset=UTF-8"),
             (b"Content-Length", str(len(request)).encode("utf-8")),
         ]
@@ -140,13 +173,11 @@
                 responseList = json.loads(response)
                 responseDict = responseList[0]
             except ValueError:
-                return (self.tr("MS Translator: Invalid response received"),
-                        False)
-            
+                return (self.tr("MS Translator: Invalid response received"), False)
+
             if "translations" not in responseDict:
-                return (self.tr("MS Translator: No translation available."),
-                        False)
-            
+                return (self.tr("MS Translator: No translation available."), False)
+
             result = ""
             translations = responseDict["translations"]
             for translation in translations:

eric ide

mercurial