After you have identified all your localized strings in the Python and template code, you need to build message files that Django can use when rendering localized strings. Creating the language files is a three-step process.
The first step is to use the make-messages application to build message files for each language that will be localized. The next step is to add the translation strings to each language message file. The final step is to compile the message files into a binary form that Django can consume.
The following sections describe the process of creating language files. Before proceeding, download the Gnu gettext package from the following location, install it, and make certain that the bin directory is in the path:
www.gnu.org/software/gettext/gettext.html
The first step in creating language files is to build the message files. A message file is a text file that contains entries that include a message ID and translation string for that particular language. Each language has one message file.
Django provides the django/bin/make-messages.py utility to create and update message files. You can also build the message file for a specific language using the -l language-code parameter. The following example shows how to build the German message file:
message-make.py -l de
You need to run the message-make.py utility for each language you want to add to your project. However, after you have added the language files, you can use the -a parameter to update all the existing message files:
message-make.py -a
Watch Out!
The message-make.py file is not automatically added to your PATH. You need to either copy it to a location that is in your path or add the full path to the django/bin/ directory in your Django installation to the PATH setting.
Message files can be built at three different levels—the Django installation, the project, and the application.
Django already comes with its own message files built and compiled. You would need to build the message file at the Django installation level only if you modified a string in the Django code or if you wanted to add a language that Django currently doesn't support. To build the message files for the Django installation, run the make-messages.py utility at the root of the Django installation. The messages are stored in the following file in the Django installation path:
django/confg/locale/language-code/LC_MESSAGES/django.po
Typically you will want to build message files for just your project. To do so, create a locale directory in the root of your project in which to store the message files. The message-make.py utility stores the message files in the following location in the project directory. However, it does not create the locale directory if it doesn't exist:
locale/language-code/LC_MESSAGES/django.po
The message-make.py utility then searches through the .py and .html files in your project and builds the message files.
You can also build message files for only one application by creating a locale directory in the application directory and then running the messages-make.py application from the application directory.
The django.po files store localized messages using the following entries for each string:
#: specifies the relative path and line numbers to the source file(s) containing the localized string.
msgid specifies the translation string as it appears in the source. You should not change this value. Django uses it to identify the string.
msgstr specifies the translated string to use for this language. It starts out empty, and you insert the translated string.
Initially the translated strings are blank. You need to add translated strings to each message file you create. To do so, open the django.po file in an editor, and add the strings.
Watch Out!
Make certain that you are using the appropriate character set for the language you are adding translations to.
After your have added the translated strings to the message files, you need to compile the message files into a binary file that can be consumed by gettext. Django provides the django/bin/compile-messages.py utility to compile the message files. The compile-messages.py file parses the django.po files and creates django.mo language files.
The compile-messages.py file must be run from the same location that the make-messages.py utility was run. After you have compiled the message file, you are ready to implement languages in your project.
Try It Yourself: Create a Language FileIn this section, you will create a German-language file in the iFriends project. You will add translations for the localized strings that you created in the two preceding "Try It Yourself" sections. Follow these steps to create the language file:
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. |