【问题标题】:Flask babel not working on simple translation?Flask babel 不能进行简单的翻译?
【发布时间】:2016-07-19 08:24:48
【问题描述】:

好的,我有以下应用:

babel.cfg

[python: **.py]
[jinja2: **/templates/**.html]
extensions=jinja2.ext.autoescape,jinja2.ext.with_

lucy/init.py

babel = Babel()

def create_app(object_name):
  app = Flask(__name__)
  app.config.from_object(object_name)

  app.register_blueprint(main_blueprint)
  app.register_blueprint(category_blueprint)
  app.register_blueprint(item_blueprint)

  babel.init_app(app)
  db.init_app(app)
  return app


@babel.localeselector
def get_locale():
  return 'de'

lucy/controllers/main.py

main_blueprint = Blueprint(
    'main',
    __name__,
    template_folder='../templates/main',
)

@main_blueprint.route('/debug')
def debug():
  print get_locale()
  return gettext('A simple string')

我运行了以下命令:

  1. pybabel 提取 -F babel.cfg -o messages.pot .
  2. pybabel init -i messages.pot -d translations -l de
  3. pybabel compile -d 翻译/

这是我的项目结构的样子:

.
|-- README.md
|-- babel.cfg
|-- fabfile.py
|-- lucy
|   |-- __init__.py
|   |-- config.py
|   |-- controllers
|   |   |-- __init__.py
|   |   |-- category.py
|   |   |-- item.py
|   |   `-- main.py
|   |-- forms.py
|   |-- models.py
|   |-- static
|   |   |-- css
|   |   `-- js
|   `-- templates
|       |-- boilerplate
|       |   |-- items.html
|       |   `-- layout.html
|       |-- category
|       |   `-- show.html
|       |-- item
|       |   |-- index.html
|       |   `-- show.html
|       `-- main
|           `-- signup.html
|-- manage.py
|-- messages.pot
|-- requirements.txt
|-- tests
|   |-- __init__.py
|   `-- test_models.py
`-- translations
    `-- de
        `-- LC_MESSAGES
            |-- messages.mo
            `-- messages.po

以下是翻译结果:

messages.pot

# Translations template for PROJECT.
# Copyright (C) 2016 ORGANIZATION
# This file is distributed under the same license as the PROJECT project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2016.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2016-07-19 16:07+0800\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.3.4\n"

#: lucy/controllers/main.py:20
msgid "A simple string"
msgstr ""

translation/de/LC_MESSAGES/message.po

# German translations for PROJECT.
# Copyright (C) 2016 ORGANIZATION
# This file is distributed under the same license as the PROJECT project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2016.
#
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2016-07-19 16:07+0800\n"
"PO-Revision-Date: 2016-07-19 16:07+0800\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language: de\n"
"Language-Team: de <LL@li.org>\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.3.4\n"

#: lucy/controllers/main.py:20
msgid "A simple string"
msgstr "german string berlin"

translation/de/LC_MESSAGES/message.mo

??,<=?M?A simple stringProject-Id-Version: PROJECT VERSION
Report-Msgid-Bugs-To: EMAIL@ADDRESS
POT-Creation-Date: 2016-07-19 16:07+0800
PO-Revision-Date: 2016-07-19 16:07+0800
Last-Translator: FULL NAME <EMAIL@ADDRESS>
Language: de
Language-Team: de <LL@li.org>
Plural-Forms: nplurals=2; plural=(n != 1)
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Generated-By: Babel 2.3.4
german string berlin

当我访问 /debug 时,我看到我的语言环境是 de。但是,我仍然看到 A simple string 正在输出。任何想法为什么?

【问题讨论】:

  • 尝试使用lazy_gettext
  • 我试过了 - 没用。为什么会有所作为?
  • 我发现出了什么问题。在我的虚拟环境库中打印了一个小时的调试语句后,我意识到他们正在寻找 Lucy/lucy 目录而不是 Lucy 目录中的翻译文件夹......
  • @Sparrowcide 你是怎么解决的?
  • 我知道我迟到了,但对我来说使用lazy_gettext 有效。问题是表单在请求生命周期之外工作,因此使用 gettext() 在实际使用之前对它们进行评估。使用lazy_gettext 他们在使用时进行评估。

标签: python flask python-babel flask-babel


【解决方案1】:

对我来说,解决方案是设置 BABEL_TRANSLATION_DIRECTORIES 变量。它包含一个字符串,其路径由';'分隔。

在我的配置中(只有一个位置):

BABEL_TRANSLATION_DIRECTORIES = '/home/work/python/project/translations'

【讨论】:

  • 这是在您的babel.cfg 文件中吗?
  • @StanJames 这将在 app.config 对象中 app.config['BABEL_TRANSLATION_DIRECTORIES'] 你应该在某个地方设置它,例如 SQLALCHEMY_DATABASE_URI - 这可能是从配置文件中读取的
【解决方案2】:

我的答案和fmelan的一样,但是我用的是绝对路径:

from flask import Flask
import os

base_dir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
app.config["BABEL_TRANSLATION_DIRECTORIES"] = os.path.join(base_dir, "app/translation")

【讨论】:

    【解决方案3】:

    从 fmelan 的回答扩展:如果您的 translations 目录与您的 flask_app.py 位于同一位置,那么您可以这样做:

    app.config['BABEL_TRANSLATION_DIRECTORIES'] = './translations'
    

    ...不要再担心绝对路径是否在应用程序运行的任何地方都有效。

    对我来说,这很好地解决了将页面部署到 PythonAnywhere 时的一个非常奇怪的行为。

    【讨论】:

    • 从 flask-babel 2.0 开始,这似乎已经是 the default
    • 也许吧,但我猜 PythonAnywhere 会混淆绝对路径,所以我必须包含它才能正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多