~metalune/simplytranslate_web

ref: cb40d99c8f22827d35558c0e34c1f50e665ae232 simplytranslate_web/main.py -rw-r--r-- 3.0 KiB
cb40d99c — fattalion Small refactor to getting form data 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from quart import Quart, render_template, request

import simplytranslate_engines.googletranslate as gtranslate
import simplytranslate_engines.libretranslate as libre

app = Quart(__name__)

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

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

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

    return None

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

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

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

    for value in supported_languages.values():
        if value.lower() == lang:
            return value

    return None

@app.route(
    "/translate/<string:from_language>/<string:to_language>/<string:input_text>/"
)
async def translate(from_language, to_language, input_text):
    return gtranslate.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"

    if translation_engine == "libre":
        supported_languages = libre.supported_languages
    elif translation_engine == "google":
        supported_languages = gtranslate.supported_languages

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

    if request.method == "GET":
        # support google format
        inp = request.args.get("text", "")

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

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

        inp = form.get("input", "")

        from_lang = form.get("from_language", "Autodetect")

        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),
            )

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

    return await render_template(
        "index.html",
        inp=inp,
        translation=translation,
        from_l=from_lang,
        to_l=to_lang,
        engine=translation_engine,
        supported_languages=supported_languages,
        use_text_fields=use_text_fields
    )

if __name__ == "__main__":
    app.run()