|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2014 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the MyMemory 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, QTimer |
|
19 |
|
20 from .TranslationEngine import TranslationEngine |
|
21 |
|
22 |
|
23 class MyMemoryEngine(TranslationEngine): |
|
24 """ |
|
25 Class implementing the translation engine for the MyMemory |
|
26 translation service. |
|
27 """ |
|
28 TranslatorUrl = "http://api.mymemory.translated.net/get" |
|
29 TranslatorLimit = 500 |
|
30 |
|
31 def __init__(self, plugin, parent=None): |
|
32 """ |
|
33 Constructor |
|
34 |
|
35 @param plugin reference to the plugin object (TranslatorPlugin) |
|
36 @param parent reference to the parent object (QObject) |
|
37 """ |
|
38 super(MyMemoryEngine, self).__init__(plugin, parent) |
|
39 |
|
40 QTimer.singleShot(0, self.availableTranslationsLoaded.emit) |
|
41 |
|
42 def engineName(self): |
|
43 """ |
|
44 Public method to return the name of the engine. |
|
45 |
|
46 @return engine name (string) |
|
47 """ |
|
48 return "mymemory" |
|
49 |
|
50 def supportedLanguages(self): |
|
51 """ |
|
52 Public method to get the supported languages. |
|
53 |
|
54 @return list of supported language codes (list of string) |
|
55 """ |
|
56 return ["ar", "be", "bg", "bs", "ca", "cs", "da", "de", "el", "en", |
|
57 "es", "et", "fi", "fr", "ga", "gl", "hi", "hr", "hu", "id", |
|
58 "is", "it", "iw", "ja", "ka", "ko", "lt", "lv", "mk", "mt", |
|
59 "nl", "no", "pl", "pt", "ro", "ru", "sk", "sl", "sq", "sr", |
|
60 "sv", "th", "tl", "tr", "uk", "vi", "zh-CN", "zh-TW", |
|
61 ] |
|
62 |
|
63 def getTranslation(self, requestObject, text, originalLanguage, |
|
64 translationLanguage): |
|
65 """ |
|
66 Public method to translate the given text. |
|
67 |
|
68 @param requestObject reference to the request object |
|
69 (TranslatorRequest) |
|
70 @param text text to be translated (string) |
|
71 @param originalLanguage language code of the original (string) |
|
72 @param translationLanguage language code of the translation (string) |
|
73 @return tuple of translated text (string) and flag indicating |
|
74 success (boolean) |
|
75 """ |
|
76 if len(text) > self.TranslatorLimit: |
|
77 return (self.tr("Only texts up to {0} characters are allowed.") |
|
78 .format(self.TranslatorLimit), False) |
|
79 |
|
80 myMemoryKey = self.plugin.getPreferences("MyMemoryKey") |
|
81 if myMemoryKey: |
|
82 keyParam = "&key={0}".format(myMemoryKey) |
|
83 else: |
|
84 keyParam = "" |
|
85 myMemoryEmail = self.plugin.getPreferences("MyMemoryEmail") |
|
86 if myMemoryEmail: |
|
87 emailParam = "&de={0}".format(myMemoryEmail) |
|
88 else: |
|
89 emailParam = "" |
|
90 params = "?of=json{3}{4}&langpair={0}|{1}&q={2}".format( |
|
91 originalLanguage, translationLanguage, text, |
|
92 keyParam, emailParam) |
|
93 url = QUrl(self.TranslatorUrl + params) |
|
94 response, ok = requestObject.get(url) |
|
95 if ok: |
|
96 response = str(response, "utf-8", "replace") |
|
97 try: |
|
98 responseDict = json.loads(response) |
|
99 except ValueError: |
|
100 return self.tr("Invalid response received"), False |
|
101 result = responseDict["responseData"]["translatedText"] |
|
102 else: |
|
103 result = response |
|
104 return result, ok |