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

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
child 7229
53054eb5b15a
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2014 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the Google V2 translation engine.
8 """
9
10 from __future__ import unicode_literals
11
12 import json
13
14 from PyQt5.QtCore import QUrl, QTimer
15
16 from .TranslationEngine import TranslationEngine
17
18
19 class GoogleV2Engine(TranslationEngine):
20 """
21 Class implementing the translation engine for the new Google
22 translation service.
23 """
24 TranslatorUrl = "https://www.googleapis.com/language/translate/v2"
25
26 def __init__(self, plugin, parent=None):
27 """
28 Constructor
29
30 @param plugin reference to the plugin object (TranslatorPlugin)
31 @param parent reference to the parent object (QObject)
32 """
33 super(GoogleV2Engine, self).__init__(plugin, parent)
34
35 QTimer.singleShot(0, self.availableTranslationsLoaded.emit)
36
37 def engineName(self):
38 """
39 Public method to return the name of the engine.
40
41 @return engine name (string)
42 """
43 return "googlev2"
44
45 def supportedLanguages(self):
46 """
47 Public method to get the supported languages.
48
49 @return list of supported language codes (list of string)
50 """
51 return ["ar", "be", "bg", "bs", "ca", "cs", "da", "de", "el", "en",
52 "es", "et", "fi", "fr", "ga", "gl", "hi", "hr", "hu", "id",
53 "is", "it", "iw", "ja", "ka", "ko", "lt", "lv", "mk", "mt",
54 "nl", "no", "pl", "pt", "ro", "ru", "sk", "sl", "sq", "sr",
55 "sv", "th", "tl", "tr", "uk", "vi", "zh-CN", "zh-TW",
56 ]
57
58 def getTranslation(self, requestObject, text, originalLanguage,
59 translationLanguage):
60 """
61 Public method to translate the given text.
62
63 @param requestObject reference to the request object
64 (TranslatorRequest)
65 @param text text to be translated (string)
66 @param originalLanguage language code of the original (string)
67 @param translationLanguage language code of the translation (string)
68 @return tuple of translated text (string) and flag indicating
69 success (boolean)
70 """
71 apiKey = self.plugin.getPreferences("GoogleV2Key")
72 if not apiKey:
73 return self.tr("A valid Google Translate key is required."), False
74
75 params = "?key={3}&source={0}&target={1}&q={2}".format(
76 originalLanguage, translationLanguage, text, apiKey)
77 url = QUrl(self.TranslatorUrl + params)
78 response, ok = requestObject.get(url)
79 if ok:
80 response = str(response, "utf-8", "replace")
81 try:
82 responseDict = json.loads(response)
83 except ValueError:
84 return self.tr("Invalid response received"), False
85
86 if "data" not in responseDict or \
87 "translations" not in responseDict["data"]:
88 return self.tr("No translation available."), False
89
90 result = ""
91 translations = responseDict["data"]["translations"]
92 for translation in translations:
93 result += translation["translatedText"]
94 if translation != translations[-1]:
95 result += "<br/>"
96 else:
97 result = response
98 return result, ok

eric ide

mercurial