|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2014 - 2017 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the Promt translation engine. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 try: |
|
12 str = unicode |
|
13 except NameError: |
|
14 pass |
|
15 |
|
16 import json |
|
17 |
|
18 from PyQt5.QtCore import QUrl, QByteArray |
|
19 |
|
20 import Utilities |
|
21 |
|
22 from .TranslationEngine import TranslationEngine |
|
23 |
|
24 |
|
25 class PromtEngine(TranslationEngine): |
|
26 """ |
|
27 Class implementing the translation engine for the Promt |
|
28 translation service. |
|
29 """ |
|
30 TranslatorUrl = \ |
|
31 "http://www.online-translator.com/services/"\ |
|
32 "TranslationService.asmx/GetTranslation" |
|
33 |
|
34 def __init__(self, plugin, parent=None): |
|
35 """ |
|
36 Constructor |
|
37 |
|
38 @param plugin reference to the plugin object (TranslatorPlugin) |
|
39 @param parent reference to the parent object (QObject) |
|
40 """ |
|
41 super(PromtEngine, self).__init__(plugin, parent) |
|
42 |
|
43 self.__mapping = { |
|
44 "de": "g", |
|
45 "en": "e", |
|
46 "es": "s", |
|
47 "fr": "f", |
|
48 "it": "i", |
|
49 "ja": "j", |
|
50 "pt": "p", |
|
51 "ru": "r", |
|
52 } |
|
53 |
|
54 def engineName(self): |
|
55 """ |
|
56 Public method to return the name of the engine. |
|
57 |
|
58 @return engine name (string) |
|
59 """ |
|
60 return "promt" |
|
61 |
|
62 def supportedLanguages(self): |
|
63 """ |
|
64 Public method to get the supported languages. |
|
65 |
|
66 @return list of supported language codes (list of string) |
|
67 """ |
|
68 return ["de", "en", "es", "fr", "it", "ja", "pt", "ru", ] |
|
69 |
|
70 def getTranslation(self, requestObject, text, originalLanguage, |
|
71 translationLanguage): |
|
72 """ |
|
73 Public method to translate the given text. |
|
74 |
|
75 @param requestObject reference to the request object |
|
76 (TranslatorRequest) |
|
77 @param text text to be translated (string) |
|
78 @param originalLanguage language code of the original (string) |
|
79 @param translationLanguage language code of the translation (string) |
|
80 @return tuple of translated text (string) and flag indicating |
|
81 success (boolean) |
|
82 """ |
|
83 encodedText = str( |
|
84 QUrl.toPercentEncoding(Utilities.html_encode(text + ".")), |
|
85 "utf-8") |
|
86 request = QByteArray( |
|
87 "{{dirCode:'{0}{1}', template:'General', text:'{2}', lang:'de'," |
|
88 " limit:3000, useAutoDetect:true, key:'', ts:'MainSite', tid:''}}" |
|
89 .format(self.__mapping[originalLanguage], |
|
90 self.__mapping[translationLanguage], |
|
91 encodedText).encode("utf-8") |
|
92 ) |
|
93 response, ok = requestObject.post(QUrl(self.TranslatorUrl), request, |
|
94 "json") |
|
95 if ok: |
|
96 try: |
|
97 responseDict = json.loads(response) |
|
98 except ValueError: |
|
99 return self.tr("Invalid response received"), False |
|
100 |
|
101 if "d" in responseDict: |
|
102 responseDict = responseDict["d"] |
|
103 |
|
104 result = responseDict["result"][:-1] # get rid of stub |
|
105 |
|
106 if responseDict["errCode"] == 0: |
|
107 if responseDict["ptsDirCode"] == "": |
|
108 result = self.tr( |
|
109 "This direction of translation is not available.") |
|
110 ok = False |
|
111 else: |
|
112 result = responseDict["errMessage"] |
|
113 ok = False |
|
114 else: |
|
115 result = response |
|
116 return result, ok |