我正在编写一个小烧瓶应用程序。这是我的Python代码:
from flask import Flask, render_template
from jinja2 import Environment
from towngen import generate_settlement
app = Flask(__name__)
jinja_env = Environment(extensions=['jinja2.ext.i18n'])
app.jinja_options['extensions'].append('jinja2.ext.i18n')
@app.route("/")
def hello_world():
return render_template("settlement.html", settlement=generate_settlement())
我使用的模板内部:
{% trans count=settlement.features|length %}
The settlement has {apnumber(count)} unique features:
{% pluralize %}
The settlement has {apnumber(count)} unique features:
{% endtrans %}
这会引发异常:
Traceback (most recent call last):
File "/Users/gru/.venvs/towngen/lib/python3.8/site-packages/flask/app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/gru/.venvs/towngen/lib/python3.8/site-packages/flask/app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "/Users/gru/.venvs/towngen/lib/python3.8/site-packages/flask/app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/gru/.venvs/towngen/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/Users/gru/.venvs/towngen/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/Users/gru/.venvs/towngen/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/gru/.venvs/towngen/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/gru/.venvs/towngen/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/Users/gru/.venvs/towngen/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/gru/.venvs/towngen/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/gru/Code/towngen/web.py", line 13, in hello_world
return render_template("settlement.html", settlement=generate_settlement())
File "/Users/gru/.venvs/towngen/lib/python3.8/site-packages/flask/templating.py", line 137, in render_template
return _render(
File "/Users/gru/.venvs/towngen/lib/python3.8/site-packages/flask/templating.py", line 120, in _render
rv = template.render(context)
File "/Users/gru/.venvs/towngen/lib/python3.8/site-packages/jinja2/environment.py", line 1090, in render
self.environment.handle_exception()
File "/Users/gru/.venvs/towngen/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception
reraise(*rewrite_traceback_stack(source=source))
File "/Users/gru/.venvs/towngen/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise
raise value.with_traceback(tb)
File "/Users/gru/Code/towngen/templates/settlement.html", line 12, in top-level template code
{% trans count=settlement.features|length %}
jinja2.exceptions.UndefinedError: 'ngettext' is undefined
我是否配置错误?我找不到可以清楚地描述如何添加该扩展的文档。
昨天我的 Jinja 3.1 项目也遇到了同样的问题,基于 那里的模板设计器文档。
我也没有看到任何明显的解释——如果还没有的话,我会考虑为此提交一张票——但事实证明,你和我在设置 i18n 扩展时都缺少一个额外的步骤。
来自扩展 API 文档的 i18n 部分:
启用后,应用程序必须在全局或渲染时提供
、gettext
以及可选的ngettext
和pgettext
的功能。npgettext
函数作为别名添加到_()
函数。gettext
如果您执行所有这些操作只是为了启用
{% pluralize %}
标记,我的建议是添加对您环境的 install_null_translations
方法的调用。这是我的做法,立即解决了问题:
from jinja2 import Environment, FileSystemLoader, select_autoescape
_env = Environment(
loader=FileSystemLoader(_TEMPLATES_DIR),
extensions=['jinja2.ext.i18n'],
autoescape=select_autoescape(default=True, default_for_string=True))
# ADD THIS NEXT LINE ---------------------------------------------------
_env.install_null_translations()