Wed, 30 Dec 2020 11:00:05 +0100
Updated copyright for 2021.
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 | |
7923
91e843545d9a
Updated copyright for 2021.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7780
diff
changeset
|
3 | # Copyright (c) 2014 - 2021 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 Promt 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 | import json |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
11 | |
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
|
12 | from PyQt5.QtCore import QUrl, QByteArray, QTimer |
6018
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
13 | |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
14 | import Utilities |
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 PromtEngine(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 Promt |
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 | """ |
7256
4ef3b78ebb4e
Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7229
diff
changeset
|
24 | TranslatorUrl = ( |
4ef3b78ebb4e
Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7229
diff
changeset
|
25 | "http://www.online-translator.com/services/" |
6018
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
26 | "TranslationService.asmx/GetTranslation" |
7256
4ef3b78ebb4e
Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7229
diff
changeset
|
27 | ) |
6018
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
28 | |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
29 | 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
|
30 | """ |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
31 | Constructor |
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 | @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
|
34 | @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
|
35 | """ |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
36 | super(PromtEngine, self).__init__(plugin, parent) |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
37 | |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
38 | self.__mapping = { |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
39 | "de": "g", |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
40 | "en": "e", |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
41 | "es": "s", |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
42 | "fr": "f", |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
43 | "it": "i", |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
44 | "ja": "j", |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
45 | "pt": "p", |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
46 | "ru": "r", |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
47 | } |
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
|
48 | |
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
|
49 | 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
|
50 | |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
51 | def engineName(self): |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
52 | """ |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
53 | 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
|
54 | |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
55 | @return engine name (string) |
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 | return "promt" |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
58 | |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
59 | def supportedLanguages(self): |
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 get the supported languages. |
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 | @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
|
64 | """ |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
65 | return ["de", "en", "es", "fr", "it", "ja", "pt", "ru", ] |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
66 | |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
67 | 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
|
68 | translationLanguage): |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
69 | """ |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
70 | 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
|
71 | |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
72 | @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
|
73 | (TranslatorRequest) |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
74 | @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
|
75 | @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
|
76 | @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
|
77 | @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
|
78 | success (boolean) |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
79 | """ |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
80 | encodedText = str( |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
81 | QUrl.toPercentEncoding(Utilities.html_encode(text + ".")), |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
82 | "utf-8") |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
83 | request = QByteArray( |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
84 | "{{dirCode:'{0}{1}', template:'General', text:'{2}', lang:'de'," |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
85 | " limit:3000, useAutoDetect:true, key:'', ts:'MainSite', tid:''}}" |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
86 | .format(self.__mapping[originalLanguage], |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
87 | self.__mapping[translationLanguage], |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
88 | encodedText).encode("utf-8") |
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 | response, ok = requestObject.post(QUrl(self.TranslatorUrl), request, |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
91 | "json") |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
92 | if ok: |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
93 | try: |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
94 | responseDict = json.loads(response) |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
95 | except ValueError: |
7748
23e98236a4c4
Translator Engines: changes the user messages to contain an engine prefix.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7360
diff
changeset
|
96 | return self.tr("Promt: Invalid response received"), False |
6018
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
97 | |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
98 | if "d" in responseDict: |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
99 | responseDict = responseDict["d"] |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
100 | |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
101 | result = responseDict["result"][:-1] # get rid of stub |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
102 | |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
103 | if responseDict["errCode"] == 0: |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
104 | if responseDict["ptsDirCode"] == "": |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
105 | result = self.tr( |
7748
23e98236a4c4
Translator Engines: changes the user messages to contain an engine prefix.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7360
diff
changeset
|
106 | "Promt: This direction of translation is not" |
23e98236a4c4
Translator Engines: changes the user messages to contain an engine prefix.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7360
diff
changeset
|
107 | " available.") |
6018
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
108 | ok = False |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
109 | else: |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
110 | result = responseDict["errMessage"] |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
111 | ok = False |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
112 | else: |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
113 | result = response |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
114 | return result, ok |