问题描述
在Flask应用程序的main.py文件中,我定义了:$ $ $ $ $ $ $ $ $ $ $ $ b ...
def somefun():
return render_template('some.html',messages = messages)
在模板文件some.html中,我用过:
< input type =' text'name ='keywords'value ='{{keywords | default('')}}'placeholder ='{{gettext('Search ...')}}'/>
这会产生一个错误:
<$ p $输入类型='text'name ='keywords'value ='{{keywords | default('')}}'placeholder ='{{gettext('Search ...') }}'/>
UndefinedError:'gettext'是未定义的
如何导入此函数以供模板使用? / b>
不幸的是,这并没有记录在案,但Flask-Babel透明地使用了。这意味着默认情况下,可以使用以下表达式函数: 也可以使用模板标签: gettext
, ngettext
和 _
{%trans num%}
有{{num}}对象。
{%pluralize%}
有{{num}}对象。
{%endtrans%}
和,等待补丁;
In my Flask application, in main.py file, I defined:
from flaskext.babel import gettext
....
def somefun():
return render_template('some.html', messages=messages)
in template file some.html, I used:
<input type='text' name='keywords' value='{{ keywords|default('') }}' placeholder='{{ gettext('Search...') }}' />
This gives an error:
<input type='text' name='keywords' value='{{ keywords|default('') }}' placeholder='{{ gettext('Search...') }}' />
UndefinedError: 'gettext' is undefined
How to import this function for template use?
Unfortunately this is not documented at all, but Flask-Babel is transparently using Jinja2's i18n extension. This means that by default, following functions for expressions are available: gettext
, ngettext
and _
.
There's also possibility to use template tags:
{% trans %}foo{% endtrans%}
{% trans num %}
There is {{ num }} object.
{% pluralize %}
There are {{ num }} objects.
{% endtrans %}
And the bug report about missing docs that's waiting for patches ;)
这篇关于Flask-Babel如何在Jinja模板文件中使用翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!