使用带有“with”和“count”关键字的blocktrans

时间:2011-03-24 07:29:45

标签: python django internationalization pluralize

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

文档仅描述了单独使用:

{% 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-templates。

2 个答案:

答案 0 :(得分:2)

是的,有可能。您只需要注意blocktrans参数的顺序:with需要在它之后立即绑定局部变量,count及其各自的变量绑定在此之后。

documentation(至少1.5版本)有几个复数示例。第二个示例(作为“更复杂的示例”引入)显示了使用withcount时的顺序:

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

答案 1 :(得分: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 %}