|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2014 - 2017 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the Glosbe 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 GlosbeEngine(TranslationEngine): |
|
24 """ |
|
25 Class implementing the translation engine for the Glosbe |
|
26 translation service. |
|
27 """ |
|
28 TranslatorUrl = "https://glosbe.com/gapi/translate" |
|
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(GlosbeEngine, 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 "glosbe" |
|
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 from ..TranslatorLanguagesDb import TranslatorLanguagesDb |
|
75 languages = TranslatorLanguagesDb(self) |
|
76 |
|
77 params = "?from={0}&dest={1}&format=json&phrase={2}".format( |
|
78 languages.convertTwoToThree(originalLanguage), |
|
79 languages.convertTwoToThree(translationLanguage), |
|
80 text) |
|
81 url = QUrl(self.TranslatorUrl + params) |
|
82 response, ok = requestObject.get(url) |
|
83 response = str(response, "utf-8", "replace") |
|
84 if ok: |
|
85 try: |
|
86 responseDict = json.loads(response) |
|
87 except ValueError: |
|
88 return self.tr("Invalid response received"), False |
|
89 |
|
90 result = "" |
|
91 for translation in responseDict["tuc"]: |
|
92 if "phrase" in translation: |
|
93 result += "<b>{0}</b>".format( |
|
94 translation["phrase"]["text"]) |
|
95 if "meanings" in translation: |
|
96 for meaning in translation["meanings"]: |
|
97 result += "<br/><i>({0})</i>".format( |
|
98 meaning["text"]) |
|
99 if translation != responseDict["tuc"][-1]: |
|
100 result += "<hr/>" |
|
101 if not result: |
|
102 result = self.tr("No translation found.") |
|
103 ok = False |
|
104 else: |
|
105 result = response |
|
106 return result, ok |