Django模板{%trans%}复数

时间:2010-05-28 14:11:42

标签: django django-templates

根据Django文档中的this section,我应该使用{% blocktrans %}来解决我需要翻译多元化的情况。但是,通过以下示例,我能做的更方便吗?

{% blocktrans count video.views.count as views %}
The video has been viewed <span>{{ views }}</span> time
{% plural %}
The video has been viewed <span>{{ views }}</span> times
{% endblocktrans %}

我尝试执行以下操作:

{% blocktrans %}time{% plural %}times{% endblocktrans %}

但它扔了TemplateSyntaxError: 'blocktrans' doesn't allow other block tags (seen u'plural') inside it

2 个答案:

答案 0 :(得分:13)

您忘记了blocktrans标记中的count variable as variable_name

该变量的值将用于检测它是否为复数。

{% blocktrans count variable as variable_name %}
    time
    {% plural %}
    {{ variable_name }} times
{% endblocktrans %}

答案 1 :(得分:-1)

您可以使用:

{% blocktrans with video.views.count|pluralize as foo  and  video.views.count as views %}
The video has been viewed <span>{{ views }}</span> time{{ foo }}
{% endblocktrans %}