The {% trans %} template tag

The {% trans %} template tag allows you to mark a string, a constant, or variable content 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 cannot handle content for translation that includes variables.

Get Django 2 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.