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

branch
eric7
changeset 8312
800c432b34c8
parent 8234
fcb6b4b96274
child 8318
962bce857696
equal deleted inserted replaced
8311:4e8b98454baa 8312:800c432b34c8
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2014 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the Google V1 translation engine.
8 """
9
10 import json
11 import re
12
13 from PyQt5.QtCore import QByteArray, QUrl, QTimer
14
15 import Utilities
16
17 from .TranslationEngine import TranslationEngine
18
19
20 class GoogleV1Engine(TranslationEngine):
21 """
22 Class implementing the translation engine for the old Google
23 translation service.
24 """
25 TranslatorUrl = "https://translate.googleapis.com/translate_a/single"
26 TextToSpeechUrl = "https://translate.google.com/translate_tts"
27 TextToSpeechLimit = 100
28
29 def __init__(self, plugin, parent=None):
30 """
31 Constructor
32
33 @param plugin reference to the plugin object (TranslatorPlugin)
34 @param parent reference to the parent object (QObject)
35 """
36 super().__init__(plugin, parent)
37
38 QTimer.singleShot(0, self.availableTranslationsLoaded.emit)
39
40 def engineName(self):
41 """
42 Public method to return the name of the engine.
43
44 @return engine name (string)
45 """
46 return "googlev1"
47
48 def supportedLanguages(self):
49 """
50 Public method to get the supported languages.
51
52 @return list of supported language codes (list of string)
53 """
54 return ["ar", "be", "bg", "bs", "ca", "cs", "da", "de", "el", "en",
55 "es", "et", "fi", "fr", "ga", "gl", "hi", "hr", "hu", "id",
56 "is", "it", "iw", "ja", "ka", "ko", "lt", "lv", "mk", "mt",
57 "nl", "no", "pl", "pt", "ro", "ru", "sk", "sl", "sq", "sr",
58 "sv", "th", "tl", "tr", "uk", "vi", "zh-CN", "zh-TW",
59 ]
60
61 def hasTTS(self):
62 """
63 Public method indicating the Text-to-Speech capability.
64
65 @return flag indicating the Text-to-Speech capability (boolean)
66 """
67 return False # doesn't work reliably
68
69 def getTranslation(self, requestObject, text, originalLanguage,
70 translationLanguage):
71 """
72 Public method to translate the given text.
73
74 @param requestObject reference to the request object
75 (TranslatorRequest)
76 @param text text to be translated (string)
77 @param originalLanguage language code of the original (string)
78 @param translationLanguage language code of the translation (string)
79 @return tuple of translated text (string) and flag indicating
80 success (boolean)
81 """
82 params = QByteArray(
83 "client=gtx&sl={0}&tl={1}&dt=t&dt=bd&ie=utf-8&oe=utf-8&q=".format(
84 originalLanguage, translationLanguage).encode("utf-8"))
85 encodedText = (
86 QByteArray(Utilities.html_encode(text).encode("utf-8"))
87 .toPercentEncoding()
88 )
89 request = params + encodedText
90 response, ok = requestObject.post(QUrl(self.TranslatorUrl), request)
91 if ok:
92 try:
93 # clean up the response
94 response = re.sub(r',{2,}', ',', response)
95 responseDict = json.loads(response)
96 except ValueError:
97 return self.tr("Google V1: Invalid response received"), False
98
99 if isinstance(responseDict, dict):
100 sentences = responseDict["sentences"]
101 result = ""
102 for sentence in sentences:
103 result += sentence["trans"].replace("\n", "<br/>")
104
105 if (
106 self.plugin.getPreferences("GoogleEnableDictionary") and
107 "dict" in responseDict
108 ):
109 dictionary = responseDict["dict"]
110 for value in dictionary:
111 result += "<hr/><u><b>{0}</b> - {1}</u><br/>".format(
112 text, value["pos"])
113 for entry in value["entry"]:
114 previous = (entry["previous_word"] + " "
115 if "previous_word" in entry else "")
116 word = entry["word"]
117 reverse = entry["reverse_translation"]
118 result += "<br/>{0}<b>{1}</b> - {2}".format(
119 previous, word, ", ".join(reverse))
120 if value != dictionary[-1]:
121 result += "<br/>"
122 elif isinstance(responseDict, list):
123 sentences = responseDict[0]
124 result = (
125 "".join([s[0] for s in sentences]).replace("\n", "<br/>")
126 )
127 if (
128 self.plugin.getPreferences("GoogleEnableDictionary") and
129 len(responseDict) > 2
130 ):
131 if not responseDict[1]:
132 result = self.tr("Google V1: No translation found.")
133 ok = False
134 else:
135 for wordTypeList in responseDict[1]:
136 result += "<hr/><u><b>{0}</b> - {1}</u>".format(
137 wordTypeList[0], wordTypeList[-2])
138 for wordsList in wordTypeList[2]:
139 reverse = wordsList[0]
140 words = wordsList[1]
141 result += "<br/><b>{0}</b> - {1}".format(
142 reverse, ", ".join(words))
143 else:
144 result = responseDict
145 else:
146 result = response
147 return result, ok
148
149 def getTextToSpeechData(self, requestObject, text, language):
150 """
151 Public method to pronounce the given text.
152
153 @param requestObject reference to the request object
154 (TranslatorRequest)
155 @param text text to be pronounced (string)
156 @param language language code of the text (string)
157 @return tuple with pronounce data (QByteArray) or error string (string)
158 and success flag (boolean)
159 """
160 text = text.split("\n\n", 1)[0]
161 if len(text) > self.TextToSpeechLimit:
162 return (self.tr("Google V1: Only texts up to {0} characters are"
163 " allowed.")
164 .format(self.TextToSpeechLimit), False)
165
166 url = QUrl(self.TextToSpeechUrl +
167 "?client=tw-ob&ie=utf-8&tl={0}&q={1}".format(
168 language, text))
169 return requestObject.get(url)

eric ide

mercurial