Flask + Flask-Security + Babel无法正常工作 [英] Flask + Flask-Security + Babel not working
问题描述
我已使用Flask + Babel + Flask Security进行了设置. 创建了所有这样的翻译:
I have setup with Flask + Babel + Flask Security. Created all translation like this:
- 根
- main.py
- 翻译
- ru
- LC_MESSAGES
- messages.mo
- messages.po
- root
- main.py
- translations
- ru
- LC_MESSAGES
- messages.mo
- messages.po
在main.py中,部分设置语言会执行:
In main.py there is part to setup language which executes:
@babel.localeselector def get_locale(): user = getattr(g, 'user', None) if user is not None: print("User locale {}".format(user.locale)) return user.locale # I put here constant to test return 'ru'
但是 http://localhost/login 保持未翻译状态.您能建议在其他地方挖掘吗?即使使用 Debug = True
But http://localhost/login stayed untranslated. Can you please advice where else to dig? No errors in logs even with Debug = True
推荐答案
我发现 Flask-Security 不适用于Babel! 从WTF处理角度来看,此请求请求已部分修复. 为了能够翻译Jinja2模板,还需要在 core.py
I have found that Flask-Security doesn't work with Babel out of box! This Pull Request fixes it partially from WTF processing perspective. To be able to translate Jinja2 templates as well following need to be amended in core.py
def render_template(self, *args, **kwargs): # Provide i18n support even if flask-babel is not installed # or enabled. kwargs['gettext'] = gettext kwargs['ngettext'] = ngettext kwargs['_'] = _ return render_template(*args, **kwargs)
这篇关于Flask + Flask-Security + Babel无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- LC_MESSAGES
- ru
- LC_MESSAGES
- ru