Plugins/UiExtensionPlugins/Translator/TranslatorEngines/DeepLEngine.py

changeset 6026
4773c9469880
parent 6018
1c858879d3d0
child 6048
82ad8ec9548c
equal deleted inserted replaced
6025:9dfb5a421a56 6026:4773c9469880
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 ["de", "en", "es", "fr", "it", "nl", "pl",] 61 return ["de", "en", "es", "fr", "it", "nl", "pl", ]
62 62
63 def getTranslation(self, requestObject, text, originalLanguage, 63 def getTranslation(self, requestObject, text, originalLanguage,
64 translationLanguage): 64 translationLanguage):
65 """ 65 """
66 Public method to translate the given text. 66 Public method to translate the given text.
86 payload = { 86 payload = {
87 "jsonrpc": "2.0", 87 "jsonrpc": "2.0",
88 "method": "LMT_handle_jobs", 88 "method": "LMT_handle_jobs",
89 "id": 1, 89 "id": 1,
90 "params": { 90 "params": {
91 "jobs": [{"kind": "default", "raw_en_sentence": s} 91 "jobs": [
92 for s in sentences 92 {"kind": "default", "raw_en_sentence": s}
93 for s in sentences
93 ], 94 ],
94 "lang": { 95 "lang": {
95 "user_preferred_langs": [ 96 "user_preferred_langs": [
96 originalLanguage.upper(), 97 originalLanguage.upper(),
97 translationLanguage.upper(), 98 translationLanguage.upper(),
111 except ValueError: 112 except ValueError:
112 return self.tr("Invalid response received from DeepL"), False 113 return self.tr("Invalid response received from DeepL"), False
113 114
114 if "error" in responseDict: 115 if "error" in responseDict:
115 return self.tr("DeepL reported an error.\nMessage: {0}")\ 116 return self.tr("DeepL reported an error.\nMessage: {0}")\
116 .format(responseDict["error"]["message"]), False 117 .format(responseDict["error"]["message"]), False
117 118
118 if "result" not in responseDict: 119 if "result" not in responseDict:
119 return self.tr("DeepL call returned an unknown result"), False 120 return self.tr("DeepL call returned an unknown result"), False
120 121
121 if not responseDict["result"]["source_lang"] or \ 122 if not responseDict["result"]["source_lang"] or \
132 # show sentence by sentence separated by a line 133 # show sentence by sentence separated by a line
133 translationStrings = [] 134 translationStrings = []
134 for translation in translations: 135 for translation in translations:
135 translationStrings.append( 136 translationStrings.append(
136 "<br/>".join( 137 "<br/>".join(
137 [s["postprocessed_sentence"] for s in sorted( 138 [
138 translation["beams"], 139 s["postprocessed_sentence"] for s in sorted(
139 key=lambda b: -1 * b["score"]) 140 translation["beams"],
141 key=lambda b: -1 * b["score"])
140 ] 142 ]
141 ) 143 )
142 ) 144 )
143 result = "<p>" + "<hr/>".join(translationStrings) + "</p>" 145 result = "<p>" + "<hr/>".join(translationStrings) + "</p>"
144 146

eric ide

mercurial