engines: add Reverso as a translation engine v2 APPLIED

: 1
 add Reverso as a translation engine

 1 files changed, 75 insertions(+), 0 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~metalune/simplytranslate-devel/patches/27490/mbox | git am -3
Learn more about email & git

[PATCH engines v2] add Reverso as a translation engine Export this patch

From: Austin Huang <im@austinhuang.me>

this fixes the language detection issue
 simplytranslate_engines/reverso.py | 75 ++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)
 create mode 100644 simplytranslate_engines/reverso.py

diff --git a/simplytranslate_engines/reverso.py b/simplytranslate_engines/reverso.py
new file mode 100644
index 0000000..e2b4f92
--- /dev/null
+++ b/simplytranslate_engines/reverso.py
@@ -0,0 +1,75 @@
import requests

class ReversoTranslateEngine:
    name = "reverso"
    display_name = "Reverso"

    def get_supported_languages(self):
        return {
            "Arabic": "ara",
            "Chinese (Simplified)": "chi",  # marketed as just "Chinese"
            "Dutch": "dut",
            "English": "eng",
            "French": "fra",
            "German": "ger",
            "Hebrew": "heb",
            "Italian": "ita",
            "Japanese": "jpn",
            "Korean": "kor",
            "Polish": "pol",
            "Portuguese": "por",
            "Romanian": "rum",
            "Russian": "rus",
            "Spanish": "spa",
            "Swedish": "swe",
            "Turkish": "tur",
            "Ukrainian": "ukr",

    def call_api(self, text, to_language, from_language):
        # `contextResults` must be False for language detection
        r = requests.post(
                "format": "text",
                "from": from_language,
                "to": to_language,
                "input": text,
                "options": {
                    "sentenceSplitter": False,
                    "origin": "translation.web",
                    "contextResults": False,
                    "languageDetection": True,
                "Content-Type": "application/json",
                "User-Agent": "",  # Either empty or a browser User-Agent
        return r

    def detect_language(self, text):
        # Any language pair works here, does not affect result
        r = self.call_api(text, "eng", "fra")
        return r["languageDetection"]["detectedLanguage"]

    def get_tts(self, text, language):
        return None

    def translate(self, text, to_language, from_language="auto"):
        if from_language == "auto":
            from_language = self.detect_language(text)
        if from_language == to_language:
            return text
        r = self.call_api(text, to_language, from_language)
        return r["translation"][0]

if __name__ == "__main__":
            "there is an impostor among us", "ger", "eng"