Wed, 30 Dec 2020 11:00:05 +0100

Detlev Offenbach <>
Wed, 30 Dec 2020 11:00:05 +0100
changeset 7923
parent 7780
child 8218

Updated copyright for 2021.

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

# Copyright (c) 2014 - 2021 Detlev Offenbach <>

Module implementing the Google V1 translation engine.

import json
import re

from PyQt5.QtCore import QByteArray, QUrl, QTimer

import Utilities

from .TranslationEngine import TranslationEngine

class GoogleV1Engine(TranslationEngine):
    Class implementing the translation engine for the old Google
    translation service.
    TranslatorUrl = ""
    TextToSpeechUrl = ""
    TextToSpeechLimit = 100
    def __init__(self, plugin, parent=None):
        @param plugin reference to the plugin object (TranslatorPlugin)
        @param parent reference to the parent object (QObject)
        super(GoogleV1Engine, self).__init__(plugin, parent)
        QTimer.singleShot(0, self.availableTranslationsLoaded.emit)
    def engineName(self):
        Public method to return the name of the engine.
        @return engine name (string)
        return "googlev1"
    def supportedLanguages(self):
        Public method to get the supported languages.
        @return list of supported language codes (list of string)
        return ["ar", "be", "bg", "bs", "ca", "cs", "da", "de", "el", "en",
                "es", "et", "fi", "fr", "ga", "gl", "hi", "hr", "hu", "id",
                "is", "it", "iw", "ja", "ka", "ko", "lt", "lv", "mk", "mt",
                "nl", "no", "pl", "pt", "ro", "ru", "sk", "sl", "sq", "sr",
                "sv", "th", "tl", "tr", "uk", "vi", "zh-CN", "zh-TW",
    def hasTTS(self):
        Public method indicating the Text-to-Speech capability.
        @return flag indicating the Text-to-Speech capability (boolean)
        return False        # doesn't work reliably
    def getTranslation(self, requestObject, text, originalLanguage,
        Public method to translate the given text.
        @param requestObject reference to the request object
        @param text text to be translated (string)
        @param originalLanguage language code of the original (string)
        @param translationLanguage language code of the translation (string)
        @return tuple of translated text (string) and flag indicating
            success (boolean)
        params = QByteArray(
                originalLanguage, translationLanguage).encode("utf-8"))
        encodedText = (
        request = params + encodedText
        response, ok =, request)
        if ok:
                # clean up the response
                response = re.sub(r',{2,}', ',', response)
                responseDict = json.loads(response)
            except ValueError:
                return"Google V1: Invalid response received"), False
            if isinstance(responseDict, dict):
                sentences = responseDict["sentences"]
                result = ""
                for sentence in sentences:
                    result += sentence["trans"].replace("\n", "<br/>")
                if (
                    self.plugin.getPreferences("GoogleEnableDictionary") and
                    "dict" in responseDict
                    dictionary = responseDict["dict"]
                    for value in dictionary:
                        result += "<hr/><u><b>{0}</b> - {1}</u><br/>".format(
                            text, value["pos"])
                        for entry in value["entry"]:
                            if "previous_word" in entry:
                                previous = entry["previous_word"] + " "
                                previous = ""
                            word = entry["word"]
                            reverse = entry["reverse_translation"]
                            result += "<br/>{0}<b>{1}</b> - {2}".format(
                                previous, word, ", ".join(reverse))
                        if value != dictionary[-1]:
                            result += "<br/>"
            elif isinstance(responseDict, list):
                sentences = responseDict[0]
                result = (
                    "".join([s[0] for s in sentences]).replace("\n", "<br/>")
                if (
                    self.plugin.getPreferences("GoogleEnableDictionary") and
                    len(responseDict) > 2
                    if not responseDict[1]:
                        result ="Google V1: No translation found.")
                        ok = False
                        for wordTypeList in responseDict[1]:
                            result += "<hr/><u><b>{0}</b> - {1}</u>".format(
                                wordTypeList[0], wordTypeList[-2])
                            for wordsList in wordTypeList[2]:
                                reverse = wordsList[0]
                                words = wordsList[1]
                                result += "<br/><b>{0}</b> - {1}".format(
                                    reverse, ", ".join(words))
                result = responseDict
            result = response
        return result, ok
    def getTextToSpeechData(self, requestObject, text, language):
        Public method to pronounce the given text.
        @param requestObject reference to the request object
        @param text text to be pronounced (string)
        @param language language code of the text (string)
        @return tuple with pronounce data (QByteArray) or error string (string)
            and success flag (boolean)
        text = text.split("\n\n", 1)[0]
        if len(text) > self.TextToSpeechLimit:
            return ("Google V1: Only texts up to {0} characters are"
                            " allowed.")
                    .format(self.TextToSpeechLimit), False)
        url = QUrl(self.TextToSpeechUrl +
                       language, text))
        return requestObject.get(url)

eric ide