在我的Flask应用程序的main.py文件中,我定义了:
from flaskext.babel import gettext
....
def somefun():
return render_template('some.html', messages=messages)在模板文件some.html中,我使用:
<input type='text' name='keywords' value='{{ keywords|default('') }}' placeholder='{{ gettext('Search...') }}' />这会给出一个错误:
<input type='text' name='keywords' value='{{ keywords|default('') }}' placeholder='{{ gettext('Search...') }}' />
UndefinedError: 'gettext' is undefined如何导入此函数以供模板使用?
发布于 2012-03-01 20:10:02
不幸的是,这根本没有文档记录,但Flask-Babel透明地使用了Jinja2's i18n extension。这意味着默认情况下,可以使用以下表达式函数:gettext、ngettext和_。
也可以使用模板标记:
{% trans %}foo{% endtrans%}
{% trans num %}
There is {{ num }} object.
{% pluralize %}
There are {{ num }} objects.
{% endtrans %}和等待补丁的bug report about missing docs ;)
https://stackoverflow.com/questions/9515156
复制相似问题