开发者

Multilingual flask application

开发者 https://www.devze.com 2023-01-10 16:46 出处:网络
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/

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.


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


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

精彩评论

暂无评论...
验证码 换一张
取 消