with app.request_context({'wsgi.url_scheme': "", 'SERVER_PORT': "", 'SERVER_NAME': "", 'REQUEST_METHOD': ""}):
from flask import g
from flask_babel import refresh
# set your user class with locale info to Flask proxy
g.user = user
# refreshing the locale and timezeone
refresh()
print lazy_gettext(u"This text should be in your language")
@ZeWaren的回答很好,如果你使用的是Flask Babel,但是如果你使用的是Flask BabelEx,就没有
force_locale
方法。在这是烧瓶BabelEx的解决方案:
注意,
.split()
在使用蓝图时非常重要。我挣扎了几个小时,因为app
对象的根路径是'主应用程序'这会让巴贝尔在'app.main.翻译“当他们在的时候”应用程序翻译'. 它会悄悄地回到NullTranslations
,也就是说,不翻译。在一种方法是设置虚拟请求上下文:
Flask Babel通过调用@巴别尔本地选择器. 我的localeselector如下所示:
^{pr2}$现在,每次更改g.user时,都应该调用refresh()来刷新Flask Babel区域设置
您还可以使用来自包
flask.ext.babel
的方法force_locale
:它的docstring是这样说的:
^{pr2}$相关问题 更多 >
编程相关推荐