17 class MicrosoftEngine(TranslationEngine): |
17 class MicrosoftEngine(TranslationEngine): |
18 """ |
18 """ |
19 Class implementing the translation engine for the Microsoft |
19 Class implementing the translation engine for the Microsoft |
20 translation service. |
20 translation service. |
21 """ |
21 """ |
|
22 |
22 TranslatorUrl = ( |
23 TranslatorUrl = ( |
23 "https://api.cognitive.microsofttranslator.com/translate" |
24 "https://api.cognitive.microsofttranslator.com/translate" "?api-version=3.0" |
24 "?api-version=3.0" |
|
25 ) |
25 ) |
26 |
26 |
27 def __init__(self, plugin, parent=None): |
27 def __init__(self, plugin, parent=None): |
28 """ |
28 """ |
29 Constructor |
29 Constructor |
30 |
30 |
31 @param plugin reference to the plugin object |
31 @param plugin reference to the plugin object |
32 @type TranslatorPlugin |
32 @type TranslatorPlugin |
33 @param parent reference to the parent object |
33 @param parent reference to the parent object |
34 @type QObject |
34 @type QObject |
35 """ |
35 """ |
36 super().__init__(plugin, parent) |
36 super().__init__(plugin, parent) |
37 |
37 |
38 self.__mappings = { |
38 self.__mappings = { |
39 "zh-CN": "zh-CHS", |
39 "zh-CN": "zh-CHS", |
40 "zh-TW": "zh-CHT", |
40 "zh-TW": "zh-CHT", |
41 } |
41 } |
42 |
42 |
43 QTimer.singleShot(0, self.availableTranslationsLoaded.emit) |
43 QTimer.singleShot(0, self.availableTranslationsLoaded.emit) |
44 |
44 |
45 def engineName(self): |
45 def engineName(self): |
46 """ |
46 """ |
47 Public method to return the name of the engine. |
47 Public method to return the name of the engine. |
48 |
48 |
49 @return engine name |
49 @return engine name |
50 @rtype str |
50 @rtype str |
51 """ |
51 """ |
52 return "microsoft" |
52 return "microsoft" |
53 |
53 |
54 def supportedLanguages(self): |
54 def supportedLanguages(self): |
55 """ |
55 """ |
56 Public method to get the supported languages. |
56 Public method to get the supported languages. |
57 |
57 |
58 @return list of supported language codes |
58 @return list of supported language codes |
59 @rtype list of str |
59 @rtype list of str |
60 """ |
60 """ |
61 return ["ar", "bg", "ca", "cs", "da", "de", "en", |
61 return [ |
62 "es", "et", "fi", "fr", "hi", "hu", "id", |
62 "ar", |
63 "it", "ja", "ko", "lt", "lv", "mt", |
63 "bg", |
64 "nl", "no", "pl", "pt", "ro", "ru", "sk", "sl", |
64 "ca", |
65 "sv", "th", "tr", "uk", "vi", "zh-CN", "zh-TW", |
65 "cs", |
66 ] |
66 "da", |
67 |
67 "de", |
|
68 "en", |
|
69 "es", |
|
70 "et", |
|
71 "fi", |
|
72 "fr", |
|
73 "hi", |
|
74 "hu", |
|
75 "id", |
|
76 "it", |
|
77 "ja", |
|
78 "ko", |
|
79 "lt", |
|
80 "lv", |
|
81 "mt", |
|
82 "nl", |
|
83 "no", |
|
84 "pl", |
|
85 "pt", |
|
86 "ro", |
|
87 "ru", |
|
88 "sk", |
|
89 "sl", |
|
90 "sv", |
|
91 "th", |
|
92 "tr", |
|
93 "uk", |
|
94 "vi", |
|
95 "zh-CN", |
|
96 "zh-TW", |
|
97 ] |
|
98 |
68 def __mapLanguageCode(self, code): |
99 def __mapLanguageCode(self, code): |
69 """ |
100 """ |
70 Private method to map a language code to the Microsoft code. |
101 Private method to map a language code to the Microsoft code. |
71 |
102 |
72 @param code language code |
103 @param code language code |
73 @type str |
104 @type str |
74 @return mapped language code |
105 @return mapped language code |
75 @rtype str |
106 @rtype str |
76 """ |
107 """ |
77 if code in self.__mappings: |
108 if code in self.__mappings: |
78 return self.__mapping[code] |
109 return self.__mapping[code] |
79 else: |
110 else: |
80 return code |
111 return code |
81 |
112 |
82 def __getClientDataAzure(self): |
113 def __getClientDataAzure(self): |
83 """ |
114 """ |
84 Private method to retrieve the client data. |
115 Private method to retrieve the client data. |
85 |
116 |
86 @return tuple giving the API subscription key, the API subscription |
117 @return tuple giving the API subscription key, the API subscription |
87 region and a flag indicating validity |
118 region and a flag indicating validity |
88 @rtype tuple of (str, str, bool) |
119 @rtype tuple of (str, str, bool) |
89 """ |
120 """ |
90 subscriptionKey = self.plugin.getPreferences("MsTranslatorKey") |
121 subscriptionKey = self.plugin.getPreferences("MsTranslatorKey") |
91 subscriptionRegion = self.plugin.getPreferences("MsTranslatorRegion") |
122 subscriptionRegion = self.plugin.getPreferences("MsTranslatorRegion") |
92 valid = bool(subscriptionKey) and bool(subscriptionRegion) |
123 valid = bool(subscriptionKey) and bool(subscriptionRegion) |
93 return subscriptionKey, subscriptionRegion, valid |
124 return subscriptionKey, subscriptionRegion, valid |
94 |
125 |
95 def getTranslation(self, requestObject, text, originalLanguage, |
126 def getTranslation( |
96 translationLanguage): |
127 self, requestObject, text, originalLanguage, translationLanguage |
|
128 ): |
97 """ |
129 """ |
98 Public method to translate the given text. |
130 Public method to translate the given text. |
99 |
131 |
100 @param requestObject reference to the request object |
132 @param requestObject reference to the request object |
101 @type TranslatorRequest |
133 @type TranslatorRequest |
102 @param text text to be translated |
134 @param text text to be translated |
103 @type str |
135 @type str |
104 @param originalLanguage language code of the original |
136 @param originalLanguage language code of the original |
106 @param translationLanguage language code of the translation |
138 @param translationLanguage language code of the translation |
107 @type str |
139 @type str |
108 @return tuple of translated text and flag indicating success |
140 @return tuple of translated text and flag indicating success |
109 @rtype tuple of (str, bool) |
141 @rtype tuple of (str, bool) |
110 """ |
142 """ |
111 subscriptionKey, subscriptionRegion, valid = ( |
143 subscriptionKey, subscriptionRegion, valid = self.__getClientDataAzure() |
112 self.__getClientDataAzure() |
|
113 ) |
|
114 if not valid: |
144 if not valid: |
115 return (self.tr("""You have not registered for the Microsoft""" |
145 return ( |
116 """ Azure Translation service."""), |
146 self.tr( |
117 False) |
147 """You have not registered for the Microsoft""" |
118 |
148 """ Azure Translation service.""" |
|
149 ), |
|
150 False, |
|
151 ) |
|
152 |
119 params = "&from={0}&to={1}".format( |
153 params = "&from={0}&to={1}".format( |
120 self.__mapLanguageCode(originalLanguage), |
154 self.__mapLanguageCode(originalLanguage), |
121 self.__mapLanguageCode(translationLanguage), |
155 self.__mapLanguageCode(translationLanguage), |
122 ) |
156 ) |
123 url = QUrl(self.TranslatorUrl + params) |
157 url = QUrl(self.TranslatorUrl + params) |
124 |
158 |
125 requestList = [{"Text": text}] |
159 requestList = [{"Text": text}] |
126 request = QByteArray(json.dumps(requestList).encode("utf-8")) |
160 request = QByteArray(json.dumps(requestList).encode("utf-8")) |
127 |
161 |
128 headers = [ |
162 headers = [ |
129 (b"Ocp-Apim-Subscription-Key", subscriptionKey.encode("utf8")), |
163 (b"Ocp-Apim-Subscription-Key", subscriptionKey.encode("utf8")), |
130 (b"Ocp-Apim-Subscription-Region", |
164 (b"Ocp-Apim-Subscription-Region", subscriptionRegion.encode("utf8")), |
131 subscriptionRegion.encode("utf8")), |
|
132 (b"Content-Type", b"application/json; charset=UTF-8"), |
165 (b"Content-Type", b"application/json; charset=UTF-8"), |
133 (b"Content-Length", str(len(request)).encode("utf-8")), |
166 (b"Content-Length", str(len(request)).encode("utf-8")), |
134 ] |
167 ] |
135 response, ok = requestObject.post( |
168 response, ok = requestObject.post( |
136 url, request, dataType="json", extraHeaders=headers |
169 url, request, dataType="json", extraHeaders=headers |