Multilingual flask application
Asked Answered
S

2

25

Is there a preferred way to make a Flask application multilingual? Ideally, the solution would enable to @app.route the same view to use different urls for each languages, like @app.route(en='/staff/', fr='/equipe/). I'm pretty confident I could hack something like that together, but an existing library would sure save me some time. Thanks.

Sike answered 6/8, 2010 at 3:53 Comment(0)
Y
25

I believe that Flask-Babel is what you are looking for.

Yulan answered 6/8, 2010 at 20:34 Comment(2)
Thanks, It looks very promising.Sike
Unfortunately, a Flask-Babel doesn't handle multi-language routes.Crimson
C
1

You can achieve this by creating a decorator that decides which route to use.

def lang_route(en, fr, *args, **kwargs):
    # Find out the user's language
    lang = "en"
    if lang == "en":
        return app.route(en, *args, **kwargs)
    if lang == "fr":
        return app.route(fr, *args, **kwargs)


@lang_route(en="/staff", fr="/equipe")
def staff():
    return "staff"
Conditioner answered 24/9, 2020 at 17:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.