|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2014 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the Translator plugin. |
|
8 """ |
|
9 |
|
10 import os |
|
11 |
|
12 from PyQt6.QtCore import pyqtSignal, QObject, QCoreApplication |
|
13 |
|
14 from EricWidgets.EricApplication import ericApp |
|
15 |
|
16 import Preferences |
|
17 |
|
18 from UiExtensionPlugins.Translator.Translator import Translator |
|
19 |
|
20 import UI.Info |
|
21 |
|
22 # Start-Of-Header |
|
23 name = "Translator Plugin" |
|
24 author = "Detlev Offenbach <detlev@die-offenbachs.de>" |
|
25 autoactivate = True |
|
26 deactivateable = True |
|
27 version = UI.Info.VersionOnly |
|
28 className = "TranslatorPlugin" |
|
29 packageName = "__core__" |
|
30 shortDescription = "Translation utility using various translators." |
|
31 longDescription = ( |
|
32 """This plug-in implements a utility to translate text using""" |
|
33 """ various online translation services.""" |
|
34 ) |
|
35 needsRestart = False |
|
36 pyqtApi = 2 |
|
37 # End-Of-Header |
|
38 |
|
39 error = "" |
|
40 |
|
41 translatorPluginObject = None |
|
42 |
|
43 |
|
44 def createTranslatorPage(configDlg): |
|
45 """ |
|
46 Module function to create the Translator configuration page. |
|
47 |
|
48 @param configDlg reference to the configuration dialog |
|
49 @type ConfigurationWidget |
|
50 @return reference to the configuration page |
|
51 @rtype TranslatorPage |
|
52 """ |
|
53 from UiExtensionPlugins.Translator.ConfigurationPage import TranslatorPage |
|
54 page = TranslatorPage.TranslatorPage(translatorPluginObject) |
|
55 return page |
|
56 |
|
57 |
|
58 def getConfigData(): |
|
59 """ |
|
60 Module function returning data as required by the configuration dialog. |
|
61 |
|
62 @return dictionary containing the relevant data |
|
63 @rtype dict |
|
64 """ |
|
65 icon = ( |
|
66 os.path.join("UiExtensionPlugins", "Translator", "icons", "flag-dark") |
|
67 if ericApp().usesDarkPalette() else |
|
68 os.path.join("UiExtensionPlugins", "Translator", "icons", "flag-light") |
|
69 ) |
|
70 return { |
|
71 "translatorPage": [ |
|
72 QCoreApplication.translate("TranslatorPlugin", |
|
73 "Translator"), |
|
74 icon, createTranslatorPage, None, None], |
|
75 } |
|
76 |
|
77 |
|
78 def prepareUninstall(): |
|
79 """ |
|
80 Module function to prepare for an uninstallation. |
|
81 """ |
|
82 Preferences.getSettings().remove(TranslatorPlugin.PreferencesKey) |
|
83 |
|
84 |
|
85 class TranslatorPlugin(QObject): |
|
86 """ |
|
87 Class implementing the Translator plug-in. |
|
88 |
|
89 @signal updateLanguages() emitted to indicate a languages update |
|
90 """ |
|
91 PreferencesKey = "Translator" |
|
92 |
|
93 updateLanguages = pyqtSignal() |
|
94 |
|
95 def __init__(self, ui): |
|
96 """ |
|
97 Constructor |
|
98 |
|
99 @param ui reference to the user interface object |
|
100 @type UI.UserInterface |
|
101 """ |
|
102 super().__init__(ui) |
|
103 self.__ui = ui |
|
104 self.__initialize() |
|
105 |
|
106 self.__defaults = { |
|
107 "OriginalLanguage": "en", |
|
108 "TranslationLanguage": "de", |
|
109 "SelectedEngine": "deepl", |
|
110 "EnabledLanguages": ["en", "de", "fr", "cs", "es", "pt", |
|
111 "ru", "tr", "zh-CN", "zh-TW"], |
|
112 # service specific settings below |
|
113 # DeepL |
|
114 "DeeplKey": "", |
|
115 # Google V1 |
|
116 "GoogleEnableDictionary": False, |
|
117 # Google V2 |
|
118 "GoogleV2Key": "", |
|
119 # IBM Watson |
|
120 "IbmUrl": "", |
|
121 "IbmKey": "", |
|
122 # Microsoft |
|
123 "MsTranslatorKey": "", |
|
124 "MsTranslatorRegion": "", |
|
125 # MyMemory |
|
126 "MyMemoryKey": "", |
|
127 "MyMemoryEmail": "", |
|
128 # Yandex |
|
129 "YandexKey": "", |
|
130 } |
|
131 |
|
132 def __initialize(self): |
|
133 """ |
|
134 Private slot to (re)initialize the plugin. |
|
135 """ |
|
136 self.__object = None |
|
137 |
|
138 def activate(self): |
|
139 """ |
|
140 Public method to activate this plugin. |
|
141 |
|
142 @return tuple of None and activation status |
|
143 @rtype tuple of (None, bool) |
|
144 """ |
|
145 global error |
|
146 error = "" # clear previous error |
|
147 |
|
148 global translatorPluginObject |
|
149 translatorPluginObject = self |
|
150 |
|
151 self.__object = Translator( |
|
152 self, ericApp().usesDarkPalette(), self.__ui) |
|
153 self.__object.activate() |
|
154 ericApp().registerPluginObject("Translator", self.__object) |
|
155 |
|
156 return None, True |
|
157 |
|
158 def deactivate(self): |
|
159 """ |
|
160 Public method to deactivate this plugin. |
|
161 """ |
|
162 ericApp().unregisterPluginObject("Translator") |
|
163 self.__object.deactivate() |
|
164 |
|
165 self.__initialize() |
|
166 |
|
167 def getPreferencesDefault(self, key): |
|
168 """ |
|
169 Public method to retrieve the various default settings. |
|
170 |
|
171 @param key the key of the value to get |
|
172 @type str |
|
173 @return the requested setting |
|
174 @rtype any |
|
175 """ |
|
176 return self.__defaults[key] |
|
177 |
|
178 def getPreferences(self, key): |
|
179 """ |
|
180 Public method to retrieve the various settings. |
|
181 |
|
182 @param key the key of the value to get |
|
183 @type str |
|
184 @return the requested setting |
|
185 @rtype any |
|
186 """ |
|
187 if key in ["EnabledLanguages"]: |
|
188 return Preferences.toList( |
|
189 Preferences.getSettings().value( |
|
190 self.PreferencesKey + "/" + key, self.__defaults[key])) |
|
191 elif key in ["GoogleEnableDictionary"]: |
|
192 return Preferences.toBool( |
|
193 Preferences.getSettings().value( |
|
194 self.PreferencesKey + "/" + key, self.__defaults[key])) |
|
195 else: |
|
196 return Preferences.getSettings().value( |
|
197 self.PreferencesKey + "/" + key, self.__defaults[key]) |
|
198 |
|
199 def setPreferences(self, key, value): |
|
200 """ |
|
201 Public method to store the various settings. |
|
202 |
|
203 @param key the key of the setting to be set |
|
204 @type str |
|
205 @param value the value to be set |
|
206 @type any |
|
207 """ |
|
208 Preferences.getSettings().setValue( |
|
209 self.PreferencesKey + "/" + key, value) |
|
210 |
|
211 if key in ["EnabledLanguages"]: |
|
212 self.updateLanguages.emit() |