Using blocktrans with "with" and "count" keywords together
Is it possible to use {% blocktrans %}
with "with" and "count" at the same time?
The documentation describes only the separate using:
{% blocktrans with foo|filter as bar and baz|filter as boo %}
{% blocktrans count var|length as count %}
I need to print a value of one variable, and the translation depends on another variable. I tried the following code:
{% 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 %}
It displays the value of a text_count
variable, but does no开发者_开发问答t translate text.
Yes, it is possible. You just need to be careful with the order of the blocktrans
arguments: with
requires a local variable binding immediately after it, and count
and its respective variable binding come after that.
The documentation (for version 1.5 at least) has a couple of pluralization examples. The second example (introduced as "A more complex example") shows the order when both with
and count
are used:
{% blocktrans with amount=article.price count years=i.length %}
That will cost $ {{ amount }} per year.
{% plural %}
That will cost $ {{ amount }} per {{ years }} years.
{% endblocktrans %}
If you don't need any other variables than the one for the counter, do not use the keyword with
at all. This is shown in the documented first example above the more complex one:
{% blocktrans count counter=list|length %}
There is only one {{ name }} object.
{% plural %}
There are {{ counter }} {{ name }} objects.
{% endblocktrans %}
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 %}
精彩评论