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: 'blocktrans' doesn't allow other block tags (seen u'plural') inside it
In Django templates, the translate tag allows you to translate either a constant string or variable content. In fact, you can mark a string to be translated via {{ _("Hello World") }} or {% trans "Hello World" %} .
The {% trans %} template tag The {% trans %} tag is useful for simple translation strings, but it cannot handle content for translation that includes variables.
These functions store a lazy reference to the string – not the actual translation. The translation itself will be done when the string is used in a string context, such as in template rendering. This is essential when calls to these functions are located in code paths that are executed at module load time.
Use the lazy versions of translation functions in django. utils. translation (easily recognizable by the lazy suffix in their names) to translate strings lazily – when the value is accessed rather than when they're called. These functions store a lazy reference to the string – not the actual translation.
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 %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With