Sat, 21 Sep 2019 20:30:56 +0200

Detlev Offenbach <>
Sat, 21 Sep 2019 20:30:56 +0200
changeset 7256
parent 7229
child 7360

Continued to resolve code style issue M841.

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

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

Module implementing the Yandex translation engine.

import json

from PyQt5.QtCore import QUrl, QByteArray, QTimer

import Utilities

from .TranslationEngine import TranslationEngine

class YandexEngine(TranslationEngine):
    Class implementing the translation engine for the Yandex
    translation service.
    TranslatorUrl = ""
    TranslatorLimit = 10000
    def __init__(self, plugin, parent=None):
        @param plugin reference to the plugin object (TranslatorPlugin)
        @param parent reference to the parent object (QObject)
        super(YandexEngine, self).__init__(plugin, parent)
        self.__errors = {
            401:"Invalid API key."),
            402:"API key has been blocked."),
            403:"Daily limit for requests has been reached."),
            404:"Daily limit for the volume of translated text"
                         " reached."),
            413:"Text size exceeds the maximum."),
            422:"Text could not be translated."),
            501:"The specified translation direction is not"
                         " supported."),
        QTimer.singleShot(0, self.availableTranslationsLoaded.emit)
    def engineName(self):
        Public method to return the name of the engine.
        @return engine name (string)
        return "yandex"
    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 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)
        if len(text) > self.TranslatorLimit:
            return ("Only texts up to {0} characters are allowed.")
                    .format(self.TranslatorLimit), False)
        apiKey = self.plugin.getPreferences("YandexKey")
        if not apiKey:
            return"A valid Yandex key is required."), False
        params = QByteArray(
                apiKey, originalLanguage, translationLanguage).encode("utf-8"))
        encodedText = (
        request = params + encodedText
        response, ok =, request)
        if ok:
                responseDict = json.loads(response)
            except ValueError:
                return"Invalid response received"), False
            if responseDict["code"] != 200:
                    error = self.__errors[responseDict["code"]]
                except KeyError:
                    error =
                        "Unknown error code ({0}) received."
                return error, False
            sentences = responseDict["text"]
            result = ""
            for sentence in sentences:
                result += sentence.replace("\n", "<br/>")
            result = response
        return result, ok

eric ide