在blocktrans中同时使用"with"和"count"关键字
可以同时使用 {% 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-templates。
2 个回答
1
这是一个关于Django框架的国际化(i18n)功能的链接,特别是讲解了一个叫做“blocktrans”的模板标签。这个标签的作用是帮助我们在网页上显示多种语言的内容,让不同语言的用户都能理解。
{% blocktrans with text_count=cnt|make_text_from_count count cnt=cnt %}
and another city
{% plural %}
and other {{ text_count }} cities
{% endblocktrans %}
5
是的,这是可能的。你只需要注意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 %}