Django internationalization on OSX Lion

Django has excellent support for website internationalization. We will now focus on the languages side of things.

First of all, we need to make sure that our Mac has the right library to generate translation files.

Installing gettext

If you run the following command in Terminal:

$ django-admin.py makemessages -a

You will probably get the following error

/bin/sh: xgettext: command not found

The reason for this is that the gettext library is not installed.

Installation from source (failed)

I have tried to download the gettext source (if not available, check the gettext site: www.gnu.org/software/gettext/)

Uncompress the package in finder, then in Terminal cd to it:

$ cd /your/path/gettext-0.18.1.1 $ ./configure $ make $ make install

But then I got the following error:

stpncpy.c:34: error: expected declaration specifiers or ‘...’ before numeric constantstpncpy.c:34: error: expected ‘)’ before ‘!=’ token stpncpy.c:34: error: expected ‘)’ before ‘?’ token make[3]: *** [stpncpy.lo] Error 1 make[2]: *** [install] Error 2 make[1]: *** [install-recursive] Error 1 make: *** [install-recursive] Error 1

It seems like it won't compile on Lion. So here's the other way.

Installation with Homebrew

So instead I'm installing it using Homebrew (a package installer for Mac OS), follow those instructions to install it.

Once brew is installed, run the following command:

$ brew install gettext $ sudo brew link gettext

Then you must add that package to your path:

$echo 'export PATH=/usr/local/Cellar/gettext/0.18.1.1/bin:$PATH' >> ~/.profile

* Make sure that your path is correct as well!

Or you will get the following error: /bin/sh: msguniq: command not found

We should be good to go to the next step now.

Generating language files

One thing the Django admin command won't do, is creating the locale folders. So in your project directory (or app directory) add the following folder structure (one for each language):

Those folder will indicate in which language your project needs to be localised. Then run the following command to generate the language files

$ django-admin.py makemessages -a

This command will compile all the languages at once (for more specific command check the Django docs).

So you will get something like that:

The PO file contains the original translation string followed byt the translated one (French example):

#: map/forms.py:9 msgid "Hello" msgstr "Bonjour"

Once you (or the translator) have completed all the translations in that PO file, you can run the compile command:

$ django-admin.py compilemessages

It will generate the compiled MO files, which will be used by Django for translating all the texts.

You will have run the makemessages command everytime you had or modify transalted string in your app, then compile everytime you change the translations.

Translation helper

I've been using a great Django app to handle translation easily, called Rosetta. It will make form fields in your admin for each translation, which ease the process rather than going through a really long text file.

Happy translating!

< / >