How to Handle Pluralization and Internationalization in Django Templates
by
This is written in the docs, but it was a first time for me to handle. Your templates can start to get very verbose when you really start supporting i18n support.Β
For strings directly in your templates you can use the blocktrans plural tag. ( Note this changes a bit with Django 3.2, blocktrans becomes blocktranslate ).
For strings directly in your templates you can use the blocktrans plural tag. ( Note this changes a bit with Django 3.2, blocktrans becomes blocktranslate ).
{% load i18n %} {% blocktrans count counter=object_list|length %} {{ object_list }}δ»Ά {% plural %} {{ object_list }}δ»Ά {% endblocktrans %}
For master data that has a dedicated DB column, you can use the get_language_code from the i18n package.
{% get_current_language as LANGUAGE_CODE %} {% if LANGUAGE_CODE != "en" %} {{ my_model.foo }} {% else %} {{ my_model.foo_en }} {% endif %}