|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2014 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the translation engine base class. |
|
8 """ |
|
9 |
|
10 import contextlib |
|
11 |
|
12 from PyQt6.QtCore import pyqtSignal, QObject |
|
13 |
|
14 |
|
15 class TranslationEngine(QObject): |
|
16 """ |
|
17 Class implementing the translation engine base class containing |
|
18 default methods. |
|
19 |
|
20 @signal availableTranslationsLoaded() emitted to indicate the availability |
|
21 of the list of supported translation languages |
|
22 """ |
|
23 availableTranslationsLoaded = pyqtSignal() |
|
24 |
|
25 def __init__(self, plugin, parent=None): |
|
26 """ |
|
27 Constructor |
|
28 |
|
29 @param plugin reference to the plugin object |
|
30 @type TranslatorPlugin |
|
31 @param parent reference to the parent object |
|
32 @type QObject |
|
33 """ |
|
34 super().__init__(parent) |
|
35 |
|
36 self.plugin = plugin |
|
37 |
|
38 def engineName(self): |
|
39 """ |
|
40 Public method to get the name of the engine. |
|
41 |
|
42 @return engine name |
|
43 @rtype str |
|
44 """ |
|
45 return "" |
|
46 |
|
47 def supportedLanguages(self): |
|
48 """ |
|
49 Public method to get the supported languages. |
|
50 |
|
51 @return list of supported language codes |
|
52 @rtype list of str |
|
53 """ |
|
54 return [] |
|
55 |
|
56 def supportedTargetLanguages(self, original): |
|
57 """ |
|
58 Public method to get a list of supported target languages for an |
|
59 original language. |
|
60 |
|
61 Note: The default implementation return the list of supported languages |
|
62 (i.e. the same as those for the source) with the given original |
|
63 removed. |
|
64 |
|
65 @param original original language |
|
66 @type str |
|
67 @return list of supported target languages for the given original |
|
68 @rtype list of str |
|
69 """ |
|
70 targetLanguages = self.supportedLanguages()[:] |
|
71 with contextlib.suppress(ValueError): |
|
72 targetLanguages.remove(original) |
|
73 |
|
74 return targetLanguages |
|
75 |
|
76 def hasTTS(self): |
|
77 """ |
|
78 Public method indicating the Text-to-Speech capability. |
|
79 |
|
80 @return flag indicating the Text-to-Speech capability |
|
81 @rtype bool |
|
82 """ |
|
83 return False |
|
84 |
|
85 def getTextToSpeechData(self, requestObject, text, language): |
|
86 """ |
|
87 Public method to pronounce the given text. |
|
88 |
|
89 @param requestObject reference to the request object |
|
90 @type TranslatorRequest |
|
91 @param text text to be pronounced |
|
92 @type str |
|
93 @param language language code of the text |
|
94 @type str |
|
95 @return tuple with pronounce data or an error string and a success flag |
|
96 @rtype tuple of (QByteArray or str, bool) |
|
97 """ |
|
98 return self.tr("No pronounce data available"), False |
|
99 |
|
100 def getTranslation(self, requestObject, text, originalLanguage, |
|
101 translationLanguage): |
|
102 """ |
|
103 Public method to translate the given text. |
|
104 |
|
105 @param requestObject reference to the request object |
|
106 @type TranslatorRequest |
|
107 @param text text to be translated |
|
108 @type str |
|
109 @param originalLanguage language code of the original |
|
110 @type str |
|
111 @param translationLanguage language code of the translation |
|
112 @type str |
|
113 @return tuple of translated text and flag indicating success |
|
114 @rtype tuple of (str, bool) |
|
115 """ |
|
116 return self.tr("No translation available"), False |