Referenced from https://docs.djangoproject.com/en/5.0/topics/i18n/translation/#internationalization-in-template-code
- Add the
{% load i18n %}
tag to enable translation at the top of the template the strings are present in taking precedent over other load tags but taking place after extend template tags. - Wrap strings in a translate tag if it is a constant string or purely variable content as shown below.
{% translate "Example string" %}
{% translate {{ title }} %}
- Wrap strings in a blocktrans tag if it is a mix of string literals and variable content.
{% blocktranslate %}This string will have {{ value }} inside.{% endblocktranslate %}
- To generate the
.po
files for translation, runmake makemessages
- To compile the
.po
files into.mo
to be able to present in in the application, runmake compilemessages
The decision on how to divide the strings into translate tags depends on the context and the structure of the sentences. Here are some general guidelines:
-
Whole sentences: Whenever possible, translate whole sentences. This helps to preserve the context and makes the translations more accurate.
-
Dynamic content: If a sentence includes dynamic content (like variables), you can use the
{% blocktrans %}
tag to translate the whole sentence, including the dynamic content. -
HTML tags: If a string includes HTML tags, you can include the tags in the
{% translate %}
or{% blocktrans %}
tag. However, be aware that this can make the translations more difficult, as the translators will need to preserve the HTML tags in their translations. -
User interface elements: Each distinct user interface element (like a button, a link, or a form field) should usually have its own
{% translate %}
tag.
In your provided code, each navigation link text is wrapped in a {% translate %}
tag, which is a good practice. This allows each link text to be translated independently, which is important because the translation of a word or phrase can depend on its context.