将 blocktrans 与“with”和“count”关键字一起使用

Gre*_*ory 5 python django internationalization pluralize

是否可以同时使用{% blocktrans %}with ”和“ count ”?

该文档仅描述了单独使用:

{% blocktrans with foo|filter as bar and baz|filter as boo %}
{% blocktrans count var|length as count %}
Run Code Online (Sandbox Code Playgroud)


我需要打印一个变量的值,而翻译取决于另一个变量。我尝试了以下代码:

{% blocktrans count cnt as count with cnt|make_text_from_count as text_count %}
    and other {{ text_count }} city
{% plural %}
    and other {{ text_count }} cities
{% endblocktrans %}
Run Code Online (Sandbox Code Playgroud)

它显示变量的值text_count,但不翻译文本。


Python 2.6.6、Django 1.3、django 模板。

Jar*_*erg 5

对的,这是可能的。您只需要注意参数的顺序blocktranswith需要紧随其后的局部变量绑定,以及count其各自的变量绑定在其后。

文档(至少 1.5 版)有几个复数示例。第二个示例(作为“更复杂的示例”介绍)显示了同时使用with和时的顺序:count

{% blocktrans with amount=article.price count years=i.length %}
That will cost $ {{ amount }} per year.
{% plural %}
That will cost $ {{ amount }} per {{ years }} years.
{% endblocktrans %}
Run Code Online (Sandbox Code Playgroud)

如果除了计数器变量之外不需要任何其他变量,则with根本不要使用该关键字。这在更复杂的上面记录的第一个示例中显示:

{% blocktrans count counter=list|length %}
There is only one {{ name }} object.
{% plural %}
There are {{ counter }} {{ name }} objects.
{% endblocktrans %}
Run Code Online (Sandbox Code Playgroud)


Ber*_*ant 1

http://docs.djangoproject.com/en/dev/topics/i18n/internationalization/#blocktrans-template-tag

{% blocktrans with text_count=cnt|make_text_from_count count cnt=cnt %}
    and another city
{% plural %}
    and other {{ text_count }} cities
{% endblocktrans %}
Run Code Online (Sandbox Code Playgroud)