|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2014 - 2017 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Package containing the various translation engines. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 import os |
|
13 |
|
14 from PyQt5.QtCore import QCoreApplication |
|
15 from PyQt5.QtGui import QIcon |
|
16 |
|
17 import UI.PixmapCache |
|
18 |
|
19 |
|
20 def supportedEngineNames(): |
|
21 """ |
|
22 Module function to get the list of supported translation engines. |
|
23 |
|
24 @return names of supported engines (list of string) |
|
25 """ |
|
26 return ["googlev1", "mymemory", "glosbe", "promt", "yandex", "googlev2", |
|
27 "microsoft", "deepl"] |
|
28 |
|
29 |
|
30 def engineDisplayName(name): |
|
31 """ |
|
32 Module function to get a translated name for an engine. |
|
33 |
|
34 @param name name of a translation engine (string) |
|
35 @return translated engine name (string) |
|
36 """ |
|
37 if name == "googlev1": |
|
38 return QCoreApplication.translate("TranslatorEngines", "Google V.1") |
|
39 elif name == "mymemory": |
|
40 return QCoreApplication.translate("TranslatorEngines", "MyMemory") |
|
41 elif name == "glosbe": |
|
42 return QCoreApplication.translate("TranslatorEngines", "Glosbe") |
|
43 elif name == "promt": |
|
44 return QCoreApplication.translate("TranslatorEngines", "PROMT") |
|
45 elif name == "yandex": |
|
46 return QCoreApplication.translate("TranslatorEngines", "Yandex") |
|
47 elif name == "googlev2": |
|
48 return QCoreApplication.translate("TranslatorEngines", "Google V.2") |
|
49 elif name == "microsoft": |
|
50 return QCoreApplication.translate("TranslatorEngines", "Microsoft") |
|
51 elif name == "deepl": |
|
52 return QCoreApplication.translate("TranslatorEngines", "DeepL") |
|
53 else: |
|
54 return QCoreApplication.translate( |
|
55 "TranslatorEngines", "Unknow translation service name ({0})")\ |
|
56 .format(name) |
|
57 |
|
58 |
|
59 def getTranslationEngine(name, plugin, parent=None): |
|
60 """ |
|
61 Module function to instantiate an engine object for the named service. |
|
62 |
|
63 @param name name of the online translation service (string) |
|
64 @param plugin reference to the plugin object (TranslatorPlugin) |
|
65 @param parent reference to the parent object |
|
66 @return translation engine (TranslatorEngine) |
|
67 """ |
|
68 if name == "googlev1": |
|
69 from .GoogleV1Engine import GoogleV1Engine |
|
70 engine = GoogleV1Engine(plugin, parent) |
|
71 elif name == "mymemory": |
|
72 from .MyMemoryEngine import MyMemoryEngine |
|
73 engine = MyMemoryEngine(plugin, parent) |
|
74 elif name == "glosbe": |
|
75 from .GlosbeEngine import GlosbeEngine |
|
76 engine = GlosbeEngine(plugin, parent) |
|
77 elif name == "promt": |
|
78 from .PromtEngine import PromtEngine |
|
79 engine = PromtEngine(plugin, parent) |
|
80 elif name == "yandex": |
|
81 from .YandexEngine import YandexEngine |
|
82 engine = YandexEngine(plugin, parent) |
|
83 elif name == "googlev2": |
|
84 from .GoogleV2Engine import GoogleV2Engine |
|
85 engine = GoogleV2Engine(plugin, parent) |
|
86 elif name == "microsoft": |
|
87 from .MicrosoftEngine import MicrosoftEngine |
|
88 engine = MicrosoftEngine(plugin, parent) |
|
89 elif name == "deepl": |
|
90 from .DeepLEngine import DeepLEngine |
|
91 engine = DeepLEngine(plugin, parent) |
|
92 else: |
|
93 engine = None |
|
94 return engine |
|
95 |
|
96 |
|
97 def getEngineIcon(name): |
|
98 """ |
|
99 Module function to get the icon of the named engine. |
|
100 |
|
101 @param name name of the translation engine |
|
102 @type str |
|
103 @return engine icon |
|
104 @rtype QIcon |
|
105 """ |
|
106 if name in supportedEngineNames(): |
|
107 return UI.PixmapCache.getIcon(os.path.join( |
|
108 os.path.dirname(__file__), "..", "icons", "engines", |
|
109 "{0}.png".format(name))) |
|
110 else: |
|
111 return QIcon() |
|
112 |
|
113 |
|
114 def getKeyUrl(name): |
|
115 """ |
|
116 Module function to get an URL to request a user key. |
|
117 |
|
118 @param name name of the online translation service (string) |
|
119 @return key request URL (string) |
|
120 """ |
|
121 if name == "mymemory": |
|
122 return "http://mymemory.translated.net/doc/keygen.php" |
|
123 elif name == "yandex": |
|
124 return "http://api.yandex.com/key/form.xml?service=trnsl" |
|
125 elif name == "googlev2": |
|
126 return "https://console.developers.google.com/" |
|
127 elif name == "microsoft": |
|
128 return "https://portal.azure.com" |
|
129 else: |
|
130 return "" |