|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2014 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the Yandex translation engine. |
|
8 """ |
|
9 |
|
10 import json |
|
11 |
|
12 from PyQt6.QtCore import QUrl, QByteArray, QTimer |
|
13 |
|
14 import Utilities |
|
15 |
|
16 from .TranslationEngine import TranslationEngine |
|
17 |
|
18 |
|
19 class YandexEngine(TranslationEngine): |
|
20 """ |
|
21 Class implementing the translation engine for the Yandex |
|
22 translation service. |
|
23 """ |
|
24 TranslatorUrl = "https://translate.yandex.net/api/v1.5/tr.json/translate" |
|
25 TranslatorLimit = 10000 |
|
26 |
|
27 def __init__(self, plugin, parent=None): |
|
28 """ |
|
29 Constructor |
|
30 |
|
31 @param plugin reference to the plugin object |
|
32 @type TranslatorPlugin |
|
33 @param parent reference to the parent object |
|
34 @type QObject |
|
35 """ |
|
36 super().__init__(plugin, parent) |
|
37 |
|
38 self.__errors = { |
|
39 401: self.tr("Yandex: Invalid API key."), |
|
40 402: self.tr("Yandex: API key has been blocked."), |
|
41 403: self.tr("Yandex: Daily limit for requests has been reached."), |
|
42 404: self.tr("Yandex: Daily limit for the volume of translated" |
|
43 " text reached."), |
|
44 413: self.tr("Yandex: Text size exceeds the maximum."), |
|
45 422: self.tr("Yandex: Text could not be translated."), |
|
46 501: self.tr("Yandex: The specified translation direction is not" |
|
47 " supported."), |
|
48 } |
|
49 |
|
50 QTimer.singleShot(0, self.availableTranslationsLoaded.emit) |
|
51 |
|
52 def engineName(self): |
|
53 """ |
|
54 Public method to return the name of the engine. |
|
55 |
|
56 @return engine name |
|
57 @rtype str |
|
58 """ |
|
59 return "yandex" |
|
60 |
|
61 def supportedLanguages(self): |
|
62 """ |
|
63 Public method to get the supported languages. |
|
64 |
|
65 @return list of supported language codes |
|
66 @rtype list of str |
|
67 """ |
|
68 return ["ar", "be", "bg", "bs", "ca", "cs", "da", "de", "el", "en", |
|
69 "es", "et", "fi", "fr", "ga", "gl", "hi", "hr", "hu", "id", |
|
70 "is", "it", "iw", "ja", "ka", "ko", "lt", "lv", "mk", "mt", |
|
71 "nl", "no", "pl", "pt", "ro", "ru", "sk", "sl", "sq", "sr", |
|
72 "sv", "th", "tl", "tr", "uk", "vi", "zh-CN", "zh-TW", |
|
73 ] |
|
74 |
|
75 def getTranslation(self, requestObject, text, originalLanguage, |
|
76 translationLanguage): |
|
77 """ |
|
78 Public method to translate the given text. |
|
79 |
|
80 @param requestObject reference to the request object |
|
81 @type TranslatorRequest |
|
82 @param text text to be translated |
|
83 @type str |
|
84 @param originalLanguage language code of the original |
|
85 @type str |
|
86 @param translationLanguage language code of the translation |
|
87 @type str |
|
88 @return tuple of translated text and flag indicating success |
|
89 @rtype tuple of (str, bool) |
|
90 """ |
|
91 if len(text) > self.TranslatorLimit: |
|
92 return ( |
|
93 self.tr("Yandex: Only texts up to {0} characters are allowed.") |
|
94 .format(self.TranslatorLimit), |
|
95 False |
|
96 ) |
|
97 |
|
98 apiKey = self.plugin.getPreferences("YandexKey") |
|
99 if not apiKey: |
|
100 return self.tr("Yandex: A valid key is required."), False |
|
101 |
|
102 params = QByteArray( |
|
103 "key={0}&lang={1}-{2}&text=".format( |
|
104 apiKey, originalLanguage, translationLanguage).encode("utf-8")) |
|
105 encodedText = ( |
|
106 QByteArray(Utilities.html_encode(text).encode("utf-8")) |
|
107 .toPercentEncoding() |
|
108 ) |
|
109 request = params + encodedText |
|
110 response, ok = requestObject.post(QUrl(self.TranslatorUrl), request) |
|
111 if ok: |
|
112 try: |
|
113 responseDict = json.loads(response) |
|
114 except ValueError: |
|
115 return self.tr("Yandex: Invalid response received"), False |
|
116 |
|
117 if responseDict["code"] != 200: |
|
118 try: |
|
119 error = self.__errors[responseDict["code"]] |
|
120 except KeyError: |
|
121 error = self.tr( |
|
122 "Yandex: Unknown error code ({0}) received." |
|
123 ).format(responseDict["code"]) |
|
124 return error, False |
|
125 |
|
126 sentences = responseDict["text"] |
|
127 result = "" |
|
128 for sentence in sentences: |
|
129 result += sentence.replace("\n", "<br/>") |
|
130 else: |
|
131 result = response |
|
132 return result, ok |