|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2014 - 2021 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 PyQt5.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 (TranslatorPlugin) |
|
30 @param parent reference to the parent object (QObject) |
|
31 """ |
|
32 super().__init__(parent) |
|
33 |
|
34 self.plugin = plugin |
|
35 |
|
36 def engineName(self): |
|
37 """ |
|
38 Public method to get the name of the engine. |
|
39 |
|
40 @return engine name (string) |
|
41 """ |
|
42 return "" |
|
43 |
|
44 def supportedLanguages(self): |
|
45 """ |
|
46 Public method to get the supported languages. |
|
47 |
|
48 @return list of supported language codes (list of string) |
|
49 """ |
|
50 return [] |
|
51 |
|
52 def supportedTargetLanguages(self, original): |
|
53 """ |
|
54 Public method to get a list of supported target languages for an |
|
55 original language. |
|
56 |
|
57 Note: The default implementation return the list of supported languages |
|
58 (i.e. the same as those for the source) with the given original |
|
59 removed. |
|
60 |
|
61 @param original original language |
|
62 @type str |
|
63 @return list of supported target languages for the given original |
|
64 @rtype list of str |
|
65 """ |
|
66 targetLanguages = self.supportedLanguages()[:] |
|
67 with contextlib.suppress(ValueError): |
|
68 targetLanguages.remove(original) |
|
69 |
|
70 return targetLanguages |
|
71 |
|
72 def hasTTS(self): |
|
73 """ |
|
74 Public method indicating the Text-to-Speech capability. |
|
75 |
|
76 @return flag indicating the Text-to-Speech capability (boolean) |
|
77 """ |
|
78 return False |
|
79 |
|
80 def getTextToSpeechData(self, requestObject, text, language): |
|
81 """ |
|
82 Public method to pronounce the given text. |
|
83 |
|
84 @param requestObject reference to the request object |
|
85 (TranslatorRequest) |
|
86 @param text text to be pronounced (string) |
|
87 @param language language code of the text (string) |
|
88 @return tuple with pronounce data (QByteArray) or error string (string) |
|
89 and success flag (boolean) |
|
90 """ |
|
91 return self.tr("No pronounce data available"), False |
|
92 |
|
93 def getTranslation(self, requestObject, text, originalLanguage, |
|
94 translationLanguage): |
|
95 """ |
|
96 Public method to translate the given text. |
|
97 |
|
98 @param requestObject reference to the request object |
|
99 (TranslatorRequest) |
|
100 @param text text to be translated (string) |
|
101 @param originalLanguage language code of the original (string) |
|
102 @param translationLanguage language code of the translation (string) |
|
103 @return tuple of translated text (string) and flag indicating |
|
104 success (boolean) |
|
105 """ |
|
106 return self.tr("No translation available"), False |