Tue, 10 Sep 2019 19:30:07 +0200

Detlev Offenbach <>
Tue, 10 Sep 2019 19:30:07 +0200
changeset 7229
parent 7192
child 7256

Removed obsolete "from __future__ import ..." statements.

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

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

Module implementing the DeepL translation engine.

import json

from PyQt5.QtCore import QUrl, QByteArray, QTimer

import Utilities

from .TranslationEngine import TranslationEngine

class DeepLEngine(TranslationEngine):
    Class implementing the translation engine for the DeepL
    translation service.
    TranslatorUrl = ""
    MaxTranslationTextLen = 30 * 1024
    def __init__(self, plugin, parent=None):
        @param plugin reference to the plugin object
        @type TranslatorPlugin
        @param parent reference to the parent object
        @type QObject
        super(DeepLEngine, 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
        @rtype str
        return "deepl"
    def supportedLanguages(self):
        Public method to get the supported languages.
        @return list of supported language codes
        @rtype list of str
        return ["de", "en", "es", "fr", "it", "nl", "pl", ]
    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.MaxTranslationTextLen:
                "Text to be translated exceeds the translation limit of {0}"
                " characters.").format(self.MaxTranslationTextLen), False
        apiKey = self.plugin.getPreferences("DeeplKey")
        if not apiKey:
            return"A valid DeepL Pro key is required."), False
        params = QByteArray(
                apiKey, originalLanguage.upper(), translationLanguage.upper())
        encodedText = QByteArray(Utilities.html_encode(text).encode("utf-8"))\
        request = params + encodedText
        response, ok =, request)
        if ok:
                responseDict = json.loads(response)
            except ValueError:
                return"Invalid response received from DeepL"), False
            if "translations" not in responseDict:
                return"DeepL call returned an unknown result"), False
            translations = responseDict["translations"]
            if len(translations) == 0:
                return"<p>No translation found</p>"), True
            # show sentence by sentence separated by a line
            result = (
                "<p>" +
                "<hr/>".join([t["text"] for t in translations]) +
            result = response
        return result, ok

eric ide