フラスコベイヤーJinjaテンプレートファイルでの翻訳の使用方法
質問
マイフラスコアプリケーションでは、main.pyファイル、i定義:
from flaskext.babel import gettext
....
def somefun():
return render_template('some.html', messages=messages)
.
テンプレートファイルの場合は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
.
テンプレート使用のためにこの機能をインポートする方法?
解決
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 ;)
所属していません StackOverflow