更换语言Flask Babel手动 [英] Change language Flask Babel manually
问题描述
我使用Flask Babel来翻译我的Flask Python应用程序。
现在我只有locale的翻译机制,但是如果用户除了他的位置想要把位置改成英文呢?有没有办法手动改变语言环境?
这是我的Flask Babel函数,它获取用户语言环境并返回语言。它位于我的 __ init __。py 文件中。
@ babel.localeselector
get get_locale ():
printLANGUAGE PT
#return'de' - >手动更改为所需语言
request.accept_languages.best_match(LANGUAGES.keys())
我真的很感激任何帮助!
最好的问候
在 Flask-Admin 文档中
$ b $ pre $
@ babel.localeselector
get get_locale():$ b $如果request.args.get('lang '):
session ['lang'] = request.args.get('lang')
return session.get('lang','en')
$ c $现在,你可以尝试法语版本的应用程序: 你可以指定默认值,也可以指定默认值。 语言通过BABEL_DEFAULT_LOCALE。
I'm using Flask Babel to translate my Flask Python app.
At moment I only have the translation mechanism by locale but what if the user besides his location wants to change the location to english? Is there any way to change the locale manually?
This is my Flask Babel function that gets the user locale and returns the language. It is on my __init__.py file.
@babel.localeselector
def get_locale():
print "LANGUAGE PT"
#return 'de' -> manually change to the desired language
request.accept_languages.best_match(LANGUAGES.keys())
I really appreciate any help!
Best regards
解决方案 A good way to do this is shown in the docs of Flask-Admin
@babel.localeselector
def get_locale():
if request.args.get('lang'):
session['lang'] = request.args.get('lang')
return session.get('lang', 'en')
Now, you could try a French version of the application at: http://localhost:5000/admin/?lang=fr.
You can still specify the default language via BABEL_DEFAULT_LOCALE.
这篇关于更换语言Flask Babel手动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!