~metalune/simplytranslate_web

de4d8b93c16e07a8aa25f90fa8d7ba832974fd90 — fattalion 3 months ago 881e63f
Allow choosing text fields or dropdowns for langs
2 files changed, 47 insertions(+), 20 deletions(-)

M main.py
M templates/index.html
M main.py => main.py +21 -9
@@ 5,12 5,21 @@ 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":
def to_lang_code(lang, supported_languages):
    lang = lang.lower()

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

    return supported_languages[long_name]
    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>/"


@@ 52,16 61,18 @@ async def index():
        if translation_engine == "libre":
            translation = libre.translate(
                inp,
                to_language=supported_languages[to_lang],
                from_language=to_short_name(from_lang, supported_languages),
                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=supported_languages[to_lang],
                from_language=to_short_name(from_lang, supported_languages),
                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,


@@ 69,7 80,8 @@ async def index():
        from_l=from_lang,
        to_l=to_lang,
        engine=translation_engine,
        supported_languages=supported_languages
        supported_languages=supported_languages,
        use_text_fields=use_text_fields
    )

if __name__ == "__main__":

M templates/index.html => templates/index.html +26 -11
@@ 20,27 20,42 @@
    <body>
        <h1>Simple Web Translator</h1>

        <form action="/?engine={{ engine }}" method="POST">
        <form action="/?engine={{ engine }}&typingiscool={{ use_text_fields }}" 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>
            <button type="submit" formaction="/?engine=google&switchengine=true&typingiscool={{ use_text_fields }}" formmethod="POST">Google</button>
            <button type="submit" formaction="/?engine=libre&switchengine=true&typingiscool={{ use_text_fields }}" formmethod="POST">LibreTranslate</button>

            <br>
            <br>

            <div id="fields_container">
                <div id="language_fields_container">
                    <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>
                    {% if use_text_fields %}

                        <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" />

                    {% else %}
                        <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>
                    {% endif %}
                </div>

                <a href="/?typingiscool={{ not use_text_fields }}">
                    {% if use_text_fields %}
                        Use dropdowns
                    {% else %}
                        Use text fields
                    {% endif %}
                </a>

                <br>

                <textarea id="input" name="input" rows="5" placeholder="text you want to translate">{{ inp }}</textarea>