Translating templates
Django offers the {% trans %}
and {% blocktrans %}
template tags to translate the strings in templates. In order to use the translation template tags, you have to add {% load i18n %}
to the top of your template to load them.
The {% trans %} template tag
The {% trans %}
template tag allows you to mark a literal for translation. Internally, Django executes gettext()
on the given text. This is how to mark a string for translation in a template:
{% trans "Text to be translated" %}
You can use as
to store the translated content in a variable that you can use throughout your template. The following example stores the translated text in a variable called greeting
:
{% trans "Hello!" as greeting %}
<h1>{{ greeting }}</h1>
The {% trans %}
tag is useful for simple translation strings, but it can’t handle content for translation that includes variables.
The {% blocktrans %} template tag
The {% blocktrans...