Internationalization in a Django Project

Today I was trying to implement internationalization in a Django project. As I am very new to Django, it took me quite a good amount of time to understand how it works. Besides, I was doing the project in Windows. So, I came across some Windows-specific problems. I followed these steps to implement it successfully:

Step 1
Check if USE_I18N is set to true in settings.py. Also add the following to the MIDDLEWARE_CLASSES:

'django.middleware.locale.LocaleMiddleware',

Step 2
Create a template (say base.html) and add the following lines:

{% load i18n %}
{% trans "Hello" %}

Step 3
Now, we need to execute django-admin.py makemessages command to make necessary message files. But this gives error in Windows because it requires the gettext utilities. Installing cygwin also solves this problem (See https://code.djangoproject.com/wiki/Localization) but I downloaded them from http://ftp.gnome.org/pub/gnome/binaries/win32/dependencies/

Download the following 3 files:

Now, extract these files in the same folder (say C:\Program Files\gettext-utils) and add “C:\Program Files\gettext-utils\bin” to the system path.

Step 4
Create message files

mkdir locale
django-admin.py makemessages -l en
django-admin.py makemessages -l fr

Step 5
Update messages and compile

django-admin.py compilemessages

Before compiling make sure that the encoding of the message file (django.po) has been set to UTF-8. I was using Eclipse and it had set the encoding to Cp1252. To change it, select the file, go to Properties and change ‘Text file encoding’ to UTF-8.

And see the magic of Django!!

Please follow these links too:
https://code.djangoproject.com/ticket/1157
http://devdoodles.wordpress.com/2009/02/14/multi-language-support-in-a-django-project/

Advertisements

One thought on “Internationalization in a Django Project

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s