This article mainly introduces how to use Django templates to work with string translation. Django is the most popular Python development framework. if you need them, refer to the Django template to use two template tags, the syntax format is somewhat different from that of Python code. To enable the template to access tags, put {% load i18n %} at the beginning of the template.
The {% trans %} template Mark translates a constant string (including single or double quotation marks) or variable content:
{% trans "This is the title." %}{% trans myvar %}
If the noop option is available, the variable query is still valid, but the translation is skipped. This is useful when the vacant content requires further translation.
{% trans "myvar" noop %}
In a string with {% trans %}, it is impossible to mix a template variable. If your translation requires a string with a variable (placeholder placeholders), use {% blocktrans % }:
{% blocktrans %}This string will have {{ value }} inside.{% endblocktrans %}
To use the template filter to translate a template expression, you need to bind the expression to a local variable in the translated text:
{% blocktrans with value|filter as myvar %}This will have {{ myvar }} inside.{% endblocktrans %}
To bind multiple expressions to the blocktrans tag, use and to separate them:
{% blocktrans with book|title as book_t and author|title as author_t %}This is {{ book_t }} by {{ author_t }}{% endblocktrans %}
To indicate the content related to the singular and plural numbers, use the {% plural %} tag between {% blocktrans %} and {% endblocktrans %} to specify the singular and plural forms. for example:
{% blocktrans count list|length as counter %}There is only one {{ name }} object.{% plural %}There are {{ counter }} {{ name }} objects.{% endblocktrans %}
The internal mechanism is that all block and embedded translation calls the corresponding gettext or ngettext.
Each RequestContext can access three specified translation variables:
- {Ages} is a list composed of a series of tuples. The first element of each tuple is the language code, and the second element is the language name expressed in this language.
- As a one-or-two string, LANGUAGE_CODE is the priority language of the current user. For example, en-us. (See how Django discovers language preferences below)
- LANGUAGE_BIDI is the description of the current region. If it is True, it is a language written from the right to the left, such as Hebrew and Arabic. If it is False, it is a language written from left to right, such as English, French, and German.
If you do not need this RequestContext extension, you can mark three values:
{% get_current_language as LANGUAGE_CODE %}{% get_available_languages as LANGUAGES %}{% get_current_language_bidi as LANGUAGE_BIDI %}
One {% load i18n %} is also required for these tags }.
The translation hook can also be used in any template block tag that accepts constant strings. In this case, the _ () expression is used to specify the translation string. for example:
{% some_special_tag _("Page not found") value|yesno:_("yes,no") %}
In this case, both the flag and the filter will see the translated strings, and there is no need to watch out for translation operations.
Note:
In this example, the translation structure misses the strings "yes, no", rather than the separate strings "yes" and "no ". The translated string will need to contain a comma so that the filter parsing code can understand how to separate parameters. For example, a German translator may translate the string "yes, no" into "ja, nein" (keep the comma intact ).