~metalune/simplytranslate_web

ref: 64239e8355ffad2750f4bc89f7e6c2079320b899 simplytranslate_web/main.py -rw-r--r-- 2.2 KiB
64239e83Alexey Yerin Remove redundant await 5 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
from quart import Quart, render_template, request

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

app = Quart(__name__)

# TODO: find a better name.
def to_short_name(long_name, supported_languages):
    if long_name == "Autodetect":
        return "auto"

    return supported_languages[long_name]

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

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

    from_lang = (await request.form).get("from_language", None)

    if from_lang == None:
        from_lang = "Autodetect"

    to_lang = (await request.form).get("to_language", None)

    if to_lang == None:
        to_lang = "English"

    translation = None

    if request.method == "POST" and not switch_engine:
        if translation_engine == "libre":
            translation = libre.translate(
                inp,
                to_language=supported_languages[to_lang],
                from_language=to_short_name(from_lang, supported_languages),
            )
        elif translation_engine == "google":
            translation = gtranslate.translate(
                inp,
                to_language=supported_languages[to_lang],
                from_language=to_short_name(from_lang, supported_languages),
            )

    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
    )

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