2012-03-01 10 views
9

、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 

テンプレートの使用のために、この関数をインポートする方法は?

答えて

18

残念ながら、これは完全には文書化されていませんが、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;)

+1

それは真実ではない、私は、任意のインポートアクションなしに「_」を使用することはできません。 –

+0

@BrentJiang Flask-Babelエクステンションを実際に初期化しましたか? – plaes

+0

@BrentJiang **コンフィグレーション** – plaes

関連する問題