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