You can also identify strings in template files that should be translated. Strings are localized in the template files the same way they are localized in Python code. However, you need to load the i18n template file into your own template first. Use the following code in your template to do so:
{% load i18n %}
The following sections discuss the different template tags and variables that you can use when localizing your templates.
The simplest way to localize a string in a template is to use the trans template tag. The trans template tag works the same way that the gettext() function does; it accepts a string and identifies that string as localized. The following is an example of using the trans template tag:
{% trans "Web Search Results" %}
By the Way
You can also use the noop argument in the trans tag to mark the string for translation but to translate it later from a variable:
{% trans "title" noop %}
The trans tag accepts only constant strings; it cannot accept variables. To localize string variables, you need to use the blocktrans tag. It identifies a block of text, including variables, as localized. The following example shows how to use the blocktrans tag:
{% blocktrans %}Title: {{ title }} {% endblocktrans %}
If you need to use some kind of template expression on a variable, such as a template filter, you need to bind the expression to a local variable to be used in the block. For example:
{% blocktrans with title|CAPFIRST as ctitle %} Title: {{ ctitle }} {% endblocktrans %}
The i18n template file also provides translation hooks within any template that accepts constant strings. To use those template hooks, use the _() syntax, as shown in the following example:
{% CustomTag _("Translation String") %}
When you use these translation hooks, the tag sees the translated string and is unaware of the translation.
RequestContext has access to the following translation-specific variables that you can use when designing your templates:
LANGUAGES is a list of two element tuples in which the first element is the language code and the second is the language name.
LANGUAGE_CODE is a string containing the preferred language code for the current user.
LANGUAGE_BIDI is Boolean. If it is True, the current language is one that is read right to left (such as Arabic or Japanese). If it is False, the current language is a left-to-right language (such as English, Spanish, or German).
For example, the following template code uses the LANGUAGE_CODE variable to display English-only code if the user's language is English:
{% ifequal LANGUAGE_CODE 'en' %} English Elements . . . {% endifequal %}
If you are not using the RequestContext extension in the view, you can also assign the translation-specific variables using the following template tag code:
{% get_available_languages as LANGUAGES %} {% get_current_language as LANGUAGE_CODE %} {% get_current_language_bidi as LANGUAGE_BIDI %}
Try It Yourself: Add a Localized String to an HTML Template FileIn this section, you will modify the iFriends_base.html template file to localize the Welcome string that is displayed before the username. Follow these steps to localize the string:
Listing 20.1. The load i18n Block and the Welcome Message Cell Entry in the iFriends/templates/iFriends_base.html File
|