Plugins/UiExtensionPlugins/Translator/TranslatorEngines/GoogleV2Engine.py

changeset 6018
1c858879d3d0
child 6048
82ad8ec9548c
equal deleted inserted replaced
6017:dab01678626d 6018:1c858879d3d0
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2014 - 2017 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
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 def engineName(self):
36 """
37 Public method to return the name of the engine.
38
39 @return engine name (string)
40 """
41 return "googlev2"
42
43 def supportedLanguages(self):
44 """
45 Public method to get the supported languages.
46
47 @return list of supported language codes (list of string)
48 """
49 return ["ar", "be", "bg", "bs", "ca", "cs", "da", "de", "el", "en",
50 "es", "et", "fi", "fr", "ga", "gl", "hi", "hr", "hu", "id",
51 "is", "it", "iw", "ja", "ka", "ko", "lt", "lv", "mk", "mt",
52 "nl", "no", "pl", "pt", "ro", "ru", "sk", "sl", "sq", "sr",
53 "sv", "th", "tl", "tr", "uk", "vi", "zh-CN", "zh-TW",
54 ]
55
56 def getTranslation(self, requestObject, text, originalLanguage,
57 translationLanguage):
58 """
59 Public method to translate the given text.
60
61 @param requestObject reference to the request object
62 (TranslatorRequest)
63 @param text text to be translated (string)
64 @param originalLanguage language code of the original (string)
65 @param translationLanguage language code of the translation (string)
66 @return tuple of translated text (string) and flag indicating
67 success (boolean)
68 """
69 apiKey = self.plugin.getPreferences("GoogleV2Key")
70 if not apiKey:
71 return self.tr("A valid Google Translate key is required."), False
72
73 params = "?key={3}&source={0}&target={1}&q={2}".format(
74 originalLanguage, translationLanguage, text, apiKey)
75 url = QUrl(self.TranslatorUrl + params)
76 response, ok = requestObject.get(url)
77 response = str(response, "utf-8", "replace")
78 if ok:
79 try:
80 responseDict = json.loads(response)
81 except ValueError:
82 return self.tr("Invalid response received"), False
83
84 if "data" not in responseDict or \
85 "translations" not in responseDict["data"]:
86 return self.tr("No translation available."), False
87
88 result = ""
89 translations = responseDict["data"]["translations"]
90 for translation in translations:
91 result += translation["translatedText"]
92 if translation != translations[-1]:
93 result += "<br/>"
94 else:
95 result = response
96 return result, ok

eric ide

mercurial