~metalune/simplytranslate_web

ecddbe5e77349aebdcd3ba8a797114b3994d9df7 — fattalion 4 months ago b21ecd5
Use select for languages and buttons for engines
2 files changed, 70 insertions(+), 35 deletions(-)

M main.py
M templates/index.html
M main.py => main.py +45 -23
@@ 5,50 5,72 @@ 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 await gtranslate.translate(
        input_text, from_language=from_language.lower(), to_language=to_language.lower()
        input_text, from_language=from_language, to_language=to_language
    )


@app.route("/", methods=["GET", "POST"])
async def index():
    translation_engine = "google"
    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"

    if request.method == "POST":
        translation_engine = (await request.form).get("engine")
        if translation_engine == None or translation_engine != "libre":
            translation_engine = "google"
    to_lang = (await request.form).get("to_language", None)

        inp = (await request.form)["input"]
        from_lang = (await request.form)["from_language"].lower()
        to_lang = (await request.form)["to_language"].lower()
    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=to_lang, from_language=from_lang
                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=to_lang, from_language=from_lang
                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
        )

    return await render_template("index.html", to_l="en", from_l="auto", engine=translation_engine)

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

M templates/index.html => templates/index.html +25 -12
@@ 1,5 1,15 @@
<!doctype html>

{% macro lang_option(lang_name, current_lang) %}
    <option value="{{ lang_name }}" {{ 'selected' if current_lang == lang_name else '' }}>{{ lang_name }}</option>
{% endmacro %}

{% macro supported_lang_options(current_lang) %}
    {% for lang_name in supported_languages.keys() %}
        {{ lang_option(lang_name, current_lang) }}
    {% endfor %}
{% endmacro %}

<html>
    <head>
        <title>SWT - Simple Web Translator</title>


@@ 10,22 20,25 @@
    <body>
        <h1>Simple Web Translator</h1>

        <form action="/" method="POST">
            <label>
                Translation Engine:
                <select name="engine">
                    <option value="google" {{'selected' if engine == 'google' else ''}}>Google</option>
                    <option value="libre" {{'selected' if engine != 'google' else ''}}>LibreTranslate</option>
                </select>
            </label>
        <form action="/?engine={{ engine }}" method="POST">
            Translation Engine:

            <button type="submit" formaction="/?engine=google&switchengine=true" formmethod="POST">Google</button>
            <button type="submit" formaction="/?engine=libre&switchengine=true" formmethod="POST">LibreTranslate</button>

            <br>
            <br>

            <div id="fields_container">

                <div id="language_fields_container">
                    <input type="text" id="from_language" name="from_language" value="{{ from_l }}" placeholder="from" />
                    <input type="text" id="to_language" name="to_language" value="{{ to_l }}" placeholder="to" />
                    <select name="from_language" id="from_language">
                        {{ lang_option('Autodetect', from_l) }}
                        {{ supported_lang_options(from_l) }}
                    </select>

                    <select name="to_language" id="to_language">
                        {{ supported_lang_options(to_l) }}
                    </select>
                </div>

                <br>


@@ 35,7 48,7 @@

            <br>

            <button type="submit">Translate!</button>
	    <button type="submit">Translate with {{ engine }}!</button>
        </form>

        <br>