Previous Page Next Page

Localizing Strings in Templates

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 trans Tag

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 blocktrans Tag

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 %}

Using Translation Hooks in Tags

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.

Accessing Languages in a Template

RequestContext has access to the following translation-specific variables that you can use when designing your templates:

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 File

In 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:

1.
Open the iFriends/templates/iFriends_base.html file in an editor.

2.
Add the following line of code, shown in Listing 20.1, to the head of the file to load the i18n template file:

{% load i18n %}

3.
Modify the Welcome string with the following trans tag, shown in Listing 20.1, to localize the string:

{% trans "Welcome" %}

4.
Save the iFriends/templates/iFriends_base.html file.

Listing 20.1. The load i18n Block and the Welcome Message Cell Entry in the iFriends/templates/iFriends_base.html File

{% load i18n %}
. . .
<td bgcolor="white"><font size="3">
    {% if user.is_authenticated %}
        {% trans "Welcome" %} {{ user.username }}.
    {% else %}
        <a href="/Login">Login</a>
    {% endif %}
</font></td>
. . .


Previous Page Next Page