|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2014 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Package containing the various translation engines. |
|
8 """ |
|
9 |
|
10 import os |
|
11 |
|
12 from PyQt6.QtCore import QCoreApplication |
|
13 from PyQt6.QtGui import QIcon |
|
14 |
|
15 from EricWidgets.EricApplication import ericApp |
|
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 |
|
25 @rtype list of str |
|
26 """ |
|
27 return [ |
|
28 "deepl", "googlev1", "googlev2", "ibm_watson", "microsoft", "mymemory", |
|
29 "yandex", |
|
30 ] |
|
31 |
|
32 |
|
33 def engineDisplayName(name): |
|
34 """ |
|
35 Module function to get a translated name for an engine. |
|
36 |
|
37 @param name name of a translation engine |
|
38 @type str |
|
39 @return translated engine name |
|
40 @rtype str |
|
41 """ |
|
42 return { |
|
43 "deepl": |
|
44 QCoreApplication.translate("TranslatorEngines", "DeepL"), |
|
45 "googlev1": |
|
46 QCoreApplication.translate("TranslatorEngines", "Google V.1"), |
|
47 "googlev2": |
|
48 QCoreApplication.translate("TranslatorEngines", "Google V.2"), |
|
49 "ibm_watson": |
|
50 QCoreApplication.translate("TranslatorEngines", "IBM Watson"), |
|
51 "microsoft": |
|
52 QCoreApplication.translate("TranslatorEngines", "Microsoft"), |
|
53 "mymemory": |
|
54 QCoreApplication.translate("TranslatorEngines", "MyMemory"), |
|
55 "yandex": |
|
56 QCoreApplication.translate("TranslatorEngines", "Yandex"), |
|
57 }.get( |
|
58 name, |
|
59 QCoreApplication.translate( |
|
60 "TranslatorEngines", "Unknow translation service name ({0})" |
|
61 ).format(name) |
|
62 ) |
|
63 |
|
64 |
|
65 def getTranslationEngine(name, plugin, parent=None): |
|
66 """ |
|
67 Module function to instantiate an engine object for the named service. |
|
68 |
|
69 @param name name of the online translation service |
|
70 @type str |
|
71 @param plugin reference to the plugin object |
|
72 @type TranslatorPlugin |
|
73 @param parent reference to the parent object |
|
74 @type QObject |
|
75 @return translation engine |
|
76 @rtype TranslatorEngine |
|
77 """ |
|
78 if name == "deepl": |
|
79 from .DeepLEngine import DeepLEngine |
|
80 engine = DeepLEngine(plugin, parent) |
|
81 elif name == "googlev1": |
|
82 from .GoogleV1Engine import GoogleV1Engine |
|
83 engine = GoogleV1Engine(plugin, parent) |
|
84 elif name == "googlev2": |
|
85 from .GoogleV2Engine import GoogleV2Engine |
|
86 engine = GoogleV2Engine(plugin, parent) |
|
87 elif name == "ibm_watson": |
|
88 from .IbmWatsonEngine import IbmWatsonEngine |
|
89 engine = IbmWatsonEngine(plugin, parent) |
|
90 elif name == "microsoft": |
|
91 from .MicrosoftEngine import MicrosoftEngine |
|
92 engine = MicrosoftEngine(plugin, parent) |
|
93 elif name == "mymemory": |
|
94 from .MyMemoryEngine import MyMemoryEngine |
|
95 engine = MyMemoryEngine(plugin, parent) |
|
96 elif name == "yandex": |
|
97 from .YandexEngine import YandexEngine |
|
98 engine = YandexEngine(plugin, parent) |
|
99 else: |
|
100 engine = None |
|
101 return engine |
|
102 |
|
103 |
|
104 def getEngineIcon(name): |
|
105 """ |
|
106 Module function to get the icon of the named engine. |
|
107 |
|
108 @param name name of the translation engine |
|
109 @type str |
|
110 @return engine icon |
|
111 @rtype QIcon |
|
112 """ |
|
113 iconSuffix = "dark" if ericApp().usesDarkPalette() else "light" |
|
114 if name in supportedEngineNames(): |
|
115 icon = UI.PixmapCache.getIcon(os.path.join( |
|
116 os.path.dirname(__file__), "..", "icons", "engines", |
|
117 "{0}-{1}".format(name, iconSuffix))) |
|
118 if icon.isNull(): |
|
119 # try variant without suffix |
|
120 icon = UI.PixmapCache.getIcon(os.path.join( |
|
121 os.path.dirname(__file__), "..", "icons", "engines", |
|
122 "{0}".format(name))) |
|
123 return icon |
|
124 else: |
|
125 return QIcon() |
|
126 |
|
127 |
|
128 def getKeyUrl(name): |
|
129 """ |
|
130 Module function to get an URL to request a user key. |
|
131 |
|
132 @param name name of the online translation service |
|
133 @type str |
|
134 @return key request URL |
|
135 @rtype str |
|
136 """ |
|
137 return { |
|
138 "deepl": |
|
139 "https://www.deepl.com/de/pro-api", |
|
140 "googlev2": |
|
141 "https://console.developers.google.com/", |
|
142 "ibm_watson": |
|
143 "https://www.ibm.com/watson/services/language-translator/", |
|
144 "microsoft": |
|
145 "https://portal.azure.com", |
|
146 "mymemory": |
|
147 "http://mymemory.translated.net/doc/keygen.php", |
|
148 "yandex": |
|
149 "http://api.yandex.com/key/form.xml?service=trnsl", |
|
150 }.get(name, "") |