eric7/Plugins/UiExtensionPlugins/Translator/TranslatorEngines/TranslationEngine.py

Mon, 13 Jun 2022 16:39:53 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 13 Jun 2022 16:39:53 +0200
branch
eric7
changeset 9148
b31f0d894b55
parent 8881
54e42bc2437a
permissions
-rw-r--r--

Translator
- removed the interface to Glosbe as they don't provide an API anymore
- removed the interface to PROMT as they don't provide a free API anymore
- upgraded the interfaces to the Google V2 and Microsoft translators

# -*- coding: utf-8 -*-

# Copyright (c) 2014 - 2022 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing the translation engine base class.
"""

import contextlib

from PyQt6.QtCore import pyqtSignal, QObject


class TranslationEngine(QObject):
    """
    Class implementing the translation engine base class containing
    default methods.
    
    @signal availableTranslationsLoaded() emitted to indicate the availability
        of the list of supported translation languages
    """
    availableTranslationsLoaded = pyqtSignal()
    
    def __init__(self, plugin, parent=None):
        """
        Constructor
        
        @param plugin reference to the plugin object
        @type TranslatorPlugin
        @param parent reference to the parent object
        @type QObject
        """
        super().__init__(parent)
        
        self.plugin = plugin
    
    def engineName(self):
        """
        Public method to get the name of the engine.
        
        @return engine name
        @rtype str
        """
        return ""
    
    def supportedLanguages(self):
        """
        Public method to get the supported languages.
        
        @return list of supported language codes
        @rtype list of str
        """
        return []
    
    def supportedTargetLanguages(self, original):
        """
        Public method to get a list of supported target languages for an
        original language.
        
        Note: The default implementation return the list of supported languages
        (i.e. the same as those for the source) with the given original
        removed.
        
        @param original original language
        @type str
        @return list of supported target languages for the given original
        @rtype list of str
        """
        targetLanguages = self.supportedLanguages()[:]
        with contextlib.suppress(ValueError):
            targetLanguages.remove(original)
        
        return targetLanguages
    
    def hasTTS(self):
        """
        Public method indicating the Text-to-Speech capability.
        
        @return flag indicating the Text-to-Speech capability
        @rtype bool
        """
        return False
    
    def getTextToSpeechData(self, requestObject, text, language):
        """
        Public method to pronounce the given text.
        
        @param requestObject reference to the request object
        @type TranslatorRequest
        @param text text to be pronounced
        @type str
        @param language language code of the text
        @type str
        @return tuple with pronounce data or an error string and a success flag
        @rtype tuple of (QByteArray or str, bool)
        """
        return self.tr("No pronounce data available"), False
    
    def getTranslation(self, requestObject, text, originalLanguage,
                       translationLanguage):
        """
        Public method to translate the given text.
        
        @param requestObject reference to the request object
        @type TranslatorRequest
        @param text text to be translated
        @type str
        @param originalLanguage language code of the original
        @type str
        @param translationLanguage language code of the translation
        @type str
        @return tuple of translated text and flag indicating success
        @rtype tuple of (str, bool)
        """
        return self.tr("No translation available"), False

eric ide

mercurial