~metalune/simplytranslate_web

5c557d2d248d65de045218add42c47fc2313d83e — fattalion 18 days ago cb40d99
Migrate to engine classes
1 files changed, 24 insertions(+), 29 deletions(-)

M main.py
M main.py => main.py +24 -29
@@ 1,28 1,36 @@
from quart import Quart, render_template, request

import simplytranslate_engines.googletranslate as gtranslate
import simplytranslate_engines.libretranslate as libre
from simplytranslate_engines.googletranslate import GoogleTranslateEngine
from simplytranslate_engines.libretranslate import LibreTranslateEngine

google_translate_engine = GoogleTranslateEngine()

engines = [google_translate_engine, LibreTranslateEngine()]

app = Quart(__name__)

def to_full_name(lang_code, supported_languages):
def to_full_name(lang_code, engine):
    lang_code = lang_code.lower()

    if lang_code == "auto":
        return "Autodetect"

    supported_languages = engine.get_supported_languages()

    for key, value in supported_languages.items():
        if value == lang_code:
            return key

    return None

def to_lang_code(lang, supported_languages):
def to_lang_code(lang, engine):
    lang = lang.lower()

    if lang == "autodetect" or lang == "auto":
        return "auto"

    supported_languages = engine.get_supported_languages()

    for key in supported_languages.keys():
        if key.lower() == lang:
            return supported_languages[key]


@@ 37,21 45,15 @@ def to_lang_code(lang, supported_languages):
    "/translate/<string:from_language>/<string:to_language>/<string:input_text>/"
)
async def translate(from_language, to_language, input_text):
    return gtranslate.translate(
    return google_translate_engine.translate(
        input_text, from_language=from_language, to_language=to_language
    )


@app.route("/", methods=["GET", "POST"])
async def index():
    translation_engine = request.args.get("engine")
    if translation_engine == None or translation_engine != "libre":
        translation_engine = "google"
    engine_name = request.args.get("engine")

    if translation_engine == "libre":
        supported_languages = libre.supported_languages
    elif translation_engine == "google":
        supported_languages = gtranslate.supported_languages
    engine = next((engine for engine in engines if engine.name == engine_name), google_translate_engine)

    switch_engine = request.args.get("switchengine", False)
    translation = None


@@ 60,9 62,9 @@ async def index():
        # support google format
        inp = request.args.get("text", "")

        from_lang = to_full_name(request.args.get("sl", "auto"), supported_languages)
        from_lang = to_full_name(request.args.get("sl", "auto"), engine)

        to_lang = to_full_name(request.args.get("tl", "en"), supported_languages)
        to_lang = to_full_name(request.args.get("tl", "en"), engine)
    elif request.method == "POST":
        form = await request.form



@@ 73,18 75,11 @@ async def index():
        to_lang = form.get("to_language", "English")

    if not (inp == "" or inp.isspace()):
        if translation_engine == "libre":
            translation = libre.translate(
                inp,
                to_language=to_lang_code(to_lang, supported_languages),
                from_language=to_lang_code(from_lang, supported_languages),
            )
        elif translation_engine == "google":
            translation = gtranslate.translate(
                inp,
                to_language=to_lang_code(to_lang, supported_languages),
                from_language=to_lang_code(from_lang, supported_languages),
            )
        translation = engine.translate(
            inp,
            to_language=to_lang_code(to_lang, engine),
            from_language=to_lang_code(from_lang, engine),
        )

    use_text_fields = request.args.get("typingiscool") == "True"



@@ 94,8 89,8 @@ async def index():
        translation=translation,
        from_l=from_lang,
        to_l=to_lang,
        engine=translation_engine,
        supported_languages=supported_languages,
        engine=engine.name,
        supported_languages=engine.get_supported_languages(),
        use_text_fields=use_text_fields
    )