What You'll Learn in This Hour |
|
Django provides full support for internationalization by allowing you to define localized strings in your Python code and template files. Using localized strings allows you to build a website that can be displayed in several different languages.
Internationalizing your website is a three-step process. First, you define which strings should be localized in different languages. Next, you build message files that contain the translated strings for each language. Finally, you configure a way to determine what language should be displayed.
The following sections discuss how to define localized strings, build the message files, and activate languages.
The first step in internationalizing your website is to define the strings that need to be translated. In your Python code, strings are identified as being localized by wrapping them in a translation function. The following sections describe how to identify strings for translation using the standard, lazy, and no-op translation methods.
The standard translation method involves using the django.utils.translation.gettext() function. The gettext() function accepts a Python string as its only argument. The string that is specified in the gettext() function is added to the message file.
For example, the following code enables a direct string and a string variable for translation:
from django.utils.translation import gettext def myView(request): rawText = 'Welcome to iFriends' transText = gettext(rawText) loginText = gettext('Please Login') . . .
If you have a lot of strings that you would like to be localized, you might want to import the gettext() function as a shorter name, as in the following example:
from django.utils.translation import gettext as _ . . . transText = _('Please Login')
You should be mindful when adding variable placeholders to localized strings that you may need to use named-string syntax. That way, when the strings are translated, the variable placeholder can move position in the translation as necessary. For example, the string Tim is my name. would be translated to Ich heisse Tim. The following code shows an example of using a named variable placeholder:
text = gettext('%(name)s is my name.') % {'name': name}
The lazy translation method involves using the django.utils.translation.gettext_lazy() function. The gettext_lazy() function accepts a Python string as its only argument. The gettext_lazy() function stores a lazy reference to the string and translates the string only if it is used in a string context.
For example, the following model definition uses the gettext_lazy() function:
from django.utils.translation import gettext_lazy as _ class Contact(models.Model): name = models.CharField(_('Name'), max_length=80) location = models.CharField(_('Location', help_text=_('Your city.'))
Watch Out!
You should always use gettext_lazy() to identify strings for translation in your Django models. You can alias gettext_lazy() to reduce the amount of text you need to type when identifying strings for translation. For example, the following code aliases gettext_lazy() as the _ character:
from django.utils.translation import gettext_lazy as _ . . . title = models.CharField(_('Title'), help_text=_('Enter a Title Here.'))
Django provides the django.utils.translation.gettext_no-op() function to identify strings as translation strings. However, they are not actually translated yet. They are translated later as a variable at the last possible point before they are rendered to the user.
You can use no-op strings for things such as constant strings, or strings stored in the database. Django will not translate a no-op strings until the last possible moment.
Try It Yourself: Add a Localized String to a Python View FunctionIn this section, you will modify the login_user() view function to localize the heading text. Follow these steps to do so:
|
By the Way
Several of the "Try it Yourself" sections in this hour add small amounts of code to Listing 20.2. Rather than listing the full contents in each "Try It Yourself" section, the full code listing is added in the final section to reduce redundancy.