Flask + Flask-Security + Babel无法正常工作 [英] Flask + Flask-Security + Babel not working

查看:105
本文介绍了Flask + Flask-Security + Babel无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆