开发者

Django template {% trans %} pluralization

开发者 https://www.devze.com 2023-01-01 04:25 出处:网络
According to this section in the Django docs I should use {% blocktrans %} for cases where I need to translate pluralizations. However, with an example like the following, isn\'t there something more

According to this section in the Django docs I should use {% blocktrans %} for cases where I need to translate pluralizations. However, with an example like the following, isn't there something more convenient I can do?

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

I tried to do the following:

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

But it threw TemplateSyntaxError: 'block开发者_开发问答trans' doesn't allow other block tags (seen u'plural') inside it


You forgot the count variable as variable_name in the blocktrans tag

The value of that variable will be used to detect if it's plural or not.

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


You can use:

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

精彩评论

暂无评论...
验证码 换一张
取 消