4

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

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

{% blocktrans with foo|filter as bar and baz|filter as boo %}
{% blocktrans count var|length as count %}


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

{% 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 %}

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


Python 2.6.6、Django 1.3、django 模板。

4

2 回答 2

3

对的,这是可能的。您只需要注意blocktrans参数的顺序:with需要紧随其后的局部变量绑定,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 %}

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

{% blocktrans count counter=list|length %}
There is only one {{ name }} object.
{% plural %}
There are {{ counter }} {{ name }} objects.
{% endblocktrans %}
于 2014-06-18T13:20:49.777 回答
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 %}
于 2011-03-24T08:41:11.297 回答