|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2014 - 2017 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 |
|
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 def engineName(self): |
|
41 """ |
|
42 Public method to return the name of the engine. |
|
43 |
|
44 @return engine name (string) |
|
45 """ |
|
46 return "mymemory" |
|
47 |
|
48 def supportedLanguages(self): |
|
49 """ |
|
50 Public method to get the supported languages. |
|
51 |
|
52 @return list of supported language codes (list of string) |
|
53 """ |
|
54 return ["ar", "be", "bg", "bs", "ca", "cs", "da", "de", "el", "en", |
|
55 "es", "et", "fi", "fr", "ga", "gl", "hi", "hr", "hu", "id", |
|
56 "is", "it", "iw", "ja", "ka", "ko", "lt", "lv", "mk", "mt", |
|
57 "nl", "no", "pl", "pt", "ro", "ru", "sk", "sl", "sq", "sr", |
|
58 "sv", "th", "tl", "tr", "uk", "vi", "zh-CN", "zh-TW", |
|
59 ] |
|
60 |
|
61 def getTranslation(self, requestObject, text, originalLanguage, |
|
62 translationLanguage): |
|
63 """ |
|
64 Public method to translate the given text. |
|
65 |
|
66 @param requestObject reference to the request object |
|
67 (TranslatorRequest) |
|
68 @param text text to be translated (string) |
|
69 @param originalLanguage language code of the original (string) |
|
70 @param translationLanguage language code of the translation (string) |
|
71 @return tuple of translated text (string) and flag indicating |
|
72 success (boolean) |
|
73 """ |
|
74 if len(text) > self.TranslatorLimit: |
|
75 return (self.tr("Only texts up to {0} characters are allowed.") |
|
76 .format(self.TranslatorLimit), False) |
|
77 |
|
78 myMemoryKey = self.plugin.getPreferences("MyMemoryKey") |
|
79 if myMemoryKey: |
|
80 keyParam = "&key={0}".format(myMemoryKey) |
|
81 else: |
|
82 keyParam = "" |
|
83 myMemoryEmail = self.plugin.getPreferences("MyMemoryEmail") |
|
84 if myMemoryEmail: |
|
85 emailParam = "&de={0}".format(myMemoryEmail) |
|
86 else: |
|
87 emailParam = "" |
|
88 params = "?of=json{3}{4}&langpair={0}|{1}&q={2}".format( |
|
89 originalLanguage, translationLanguage, text, |
|
90 keyParam, emailParam) |
|
91 url = QUrl(self.TranslatorUrl + params) |
|
92 response, ok = requestObject.get(url) |
|
93 response = str(response, "utf-8", "replace") |
|
94 if ok: |
|
95 try: |
|
96 responseDict = json.loads(response) |
|
97 except ValueError: |
|
98 return self.tr("Invalid response received"), False |
|
99 result = responseDict["responseData"]["translatedText"] |
|
100 else: |
|
101 result = response |
|
102 return result, ok |