Sun, 16 May 2021 20:07:24 +0200
Replaced all imports of PyQt5 to PyQt6 and started to replace code using obsoleted methods and adapt to the PyQt6 enum usage.
6018
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
1 | # -*- coding: utf-8 -*- |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
2 | |
7923
91e843545d9a
Updated copyright for 2021.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7780
diff
changeset
|
3 | # Copyright (c) 2014 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
6018
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
4 | # |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
5 | |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
6 | """ |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
7 | Module implementing the translation engine base class. |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
8 | """ |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
9 | |
8240
93b8a353c4bf
Applied some more code simplifications suggested by the new Simplify checker (Y105: use contextlib.suppress) (batch 1).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8218
diff
changeset
|
10 | import contextlib |
93b8a353c4bf
Applied some more code simplifications suggested by the new Simplify checker (Y105: use contextlib.suppress) (batch 1).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8218
diff
changeset
|
11 | |
8318
962bce857696
Replaced all imports of PyQt5 to PyQt6 and started to replace code using obsoleted methods and adapt to the PyQt6 enum usage.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
12 | from PyQt6.QtCore import pyqtSignal, QObject |
6018
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
13 | |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
14 | |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
15 | class TranslationEngine(QObject): |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
16 | """ |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
17 | Class implementing the translation engine base class containing |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
18 | default methods. |
6412
d71b094845e7
Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6411
diff
changeset
|
19 | |
d71b094845e7
Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6411
diff
changeset
|
20 | @signal availableTranslationsLoaded() emitted to indicate the availability |
d71b094845e7
Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6411
diff
changeset
|
21 | of the list of supported translation languages |
6018
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
22 | """ |
6412
d71b094845e7
Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6411
diff
changeset
|
23 | availableTranslationsLoaded = pyqtSignal() |
d71b094845e7
Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6411
diff
changeset
|
24 | |
6018
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
25 | def __init__(self, plugin, parent=None): |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
26 | """ |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
27 | Constructor |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
28 | |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
29 | @param plugin reference to the plugin object (TranslatorPlugin) |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
30 | @param parent reference to the parent object (QObject) |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
31 | """ |
8218
7c09585bd960
Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7923
diff
changeset
|
32 | super().__init__(parent) |
6018
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
33 | |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
34 | self.plugin = plugin |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
35 | |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
36 | def engineName(self): |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
37 | """ |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
38 | Public method to get the name of the engine. |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
39 | |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
40 | @return engine name (string) |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
41 | """ |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
42 | return "" |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
43 | |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
44 | def supportedLanguages(self): |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
45 | """ |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
46 | Public method to get the supported languages. |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
47 | |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
48 | @return list of supported language codes (list of string) |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
49 | """ |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
50 | return [] |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
51 | |
6411
7fd9b7ecbcfe
Translator: added a translation engine for the IBM Watson Language Translator.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6048
diff
changeset
|
52 | def supportedTargetLanguages(self, original): |
7fd9b7ecbcfe
Translator: added a translation engine for the IBM Watson Language Translator.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6048
diff
changeset
|
53 | """ |
7fd9b7ecbcfe
Translator: added a translation engine for the IBM Watson Language Translator.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6048
diff
changeset
|
54 | Public method to get a list of supported target languages for an |
7fd9b7ecbcfe
Translator: added a translation engine for the IBM Watson Language Translator.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6048
diff
changeset
|
55 | original language. |
7fd9b7ecbcfe
Translator: added a translation engine for the IBM Watson Language Translator.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6048
diff
changeset
|
56 | |
6412
d71b094845e7
Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6411
diff
changeset
|
57 | Note: The default implementation return the list of supported languages |
d71b094845e7
Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6411
diff
changeset
|
58 | (i.e. the same as those for the source) with the given original |
d71b094845e7
Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6411
diff
changeset
|
59 | removed. |
d71b094845e7
Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6411
diff
changeset
|
60 | |
6411
7fd9b7ecbcfe
Translator: added a translation engine for the IBM Watson Language Translator.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6048
diff
changeset
|
61 | @param original original language |
7fd9b7ecbcfe
Translator: added a translation engine for the IBM Watson Language Translator.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6048
diff
changeset
|
62 | @type str |
7fd9b7ecbcfe
Translator: added a translation engine for the IBM Watson Language Translator.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6048
diff
changeset
|
63 | @return list of supported target languages for the given original |
6412
d71b094845e7
Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6411
diff
changeset
|
64 | @rtype list of str |
6411
7fd9b7ecbcfe
Translator: added a translation engine for the IBM Watson Language Translator.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6048
diff
changeset
|
65 | """ |
6412
d71b094845e7
Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6411
diff
changeset
|
66 | targetLanguages = self.supportedLanguages()[:] |
8240
93b8a353c4bf
Applied some more code simplifications suggested by the new Simplify checker (Y105: use contextlib.suppress) (batch 1).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8218
diff
changeset
|
67 | with contextlib.suppress(ValueError): |
6412
d71b094845e7
Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6411
diff
changeset
|
68 | targetLanguages.remove(original) |
d71b094845e7
Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6411
diff
changeset
|
69 | |
d71b094845e7
Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6411
diff
changeset
|
70 | return targetLanguages |
6411
7fd9b7ecbcfe
Translator: added a translation engine for the IBM Watson Language Translator.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6048
diff
changeset
|
71 | |
6018
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
72 | def hasTTS(self): |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
73 | """ |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
74 | Public method indicating the Text-to-Speech capability. |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
75 | |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
76 | @return flag indicating the Text-to-Speech capability (boolean) |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
77 | """ |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
78 | return False |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
79 | |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
80 | def getTextToSpeechData(self, requestObject, text, language): |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
81 | """ |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
82 | Public method to pronounce the given text. |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
83 | |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
84 | @param requestObject reference to the request object |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
85 | (TranslatorRequest) |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
86 | @param text text to be pronounced (string) |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
87 | @param language language code of the text (string) |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
88 | @return tuple with pronounce data (QByteArray) or error string (string) |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
89 | and success flag (boolean) |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
90 | """ |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
91 | return self.tr("No pronounce data available"), False |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
92 | |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
93 | def getTranslation(self, requestObject, text, originalLanguage, |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
94 | translationLanguage): |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
95 | """ |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
96 | Public method to translate the given text. |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
97 | |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
98 | @param requestObject reference to the request object |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
99 | (TranslatorRequest) |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
100 | @param text text to be translated (string) |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
101 | @param originalLanguage language code of the original (string) |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
102 | @param translationLanguage language code of the translation (string) |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
103 | @return tuple of translated text (string) and flag indicating |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
104 | success (boolean) |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
105 | """ |
1c858879d3d0
Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
106 | return self.tr("No translation available"), False |