Sunday, June 3, 2012

Django 1.3 Language Translation

Translating Templates with Django

Below is steps for translating one Django websites into Norwegian.

Settings

As I'm only providing translation for Norwegian, I set the following in my settings.py file:
LANGUAGE_CODE = 'en-gb'

LANGUAGES = (
  ('nb', 'Norwegian Bokmal'),
  ('nn', 'Norwegian Nynorsk'),
  ('en-gb', 'English'),
)
This defaults to English, but limits the list of languages on offer. I also added LocaleMiddleware to my MIDDLEWARE_CLASSES:
MIDDLEWARE_CLASSES = (
    'django.middleware.csrf.CsrfViewMiddleware',                      
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
   ...
)

Mark Up Template

Open up the template to translate, and add the trans tag around the text you want translating. So,
<h2>Part 9 - Three Toms and Rolls</h2>
<p>This section contains the first exercises containing snare drum rolls.</p>
<h2>Part 10 - Ride Cymbal and Cut Common</h2>
<p>This part introduces the ride cymbal to the drum kit</p>
becomes
<h2>{% trans "Part" %} 9 - {% trans "Three Toms and Rolls" %}</h2>
<p>{% trans "This section contains the first exercises containing snare drum rolls." %}</p>
<h2>{% trans "Part" %} 10 - {% trans "Ride Cymbal and Cut Common" %}</h2>
<p>{% trans "This part introduces the ride cymbal to the drum kit" %}</p>
Add {% load i18n %} at the top of the template too.

Run make-messages.py

Once this is done, you need to run a script which will create message files. You need to run dango-admin.py makemessages in the same directory as your settings file, after creating a locale directory
me@coderpriyu:~/web/progperc/site$ export PYTHONPATH=~/web/site
me@coderpriyu:~/web/progperc/site$ ~/web/progperc/django/bin/django-admin.py makemessages -l nb
Error: This script should be run from the Django SVN tree or your project or app tree. If you did indeed 
run it from the SVN checkout or your project or application, maybe you are just missing the conf/locale 
(in the django tree) or locale (for project and application) directory? It is not created automatically, you 
have to create it by hand if you want to enable i18n for your project or application.
This error means that we need to create a locale directory in the directory that contains settings.py:
me@coderpriyu:~/web/progperc/site$ mkdir locale
me@coderpriyu:~/web/progperc/site$ ~/web/progperc/django/bin/django-admin.py makemessages -l nb
processing language nb
Error: errors happened while running xgettext on coverage.py
/bin/sh: xgettext: not found
This error occurs because we don't have the xgettext command installed. I'm on Ubuntu 11.04, so sudo apt-get install gettext should do the trick.

Success!:
me@coderpriyu:~/web/progperc/site$ ~/web/progperc/django/bin/django-admin.py makemessages -l nb
processing language nb
I now have a file, in ~/web/site/locale/nb/LC_MESSAGES called django.po, with contents similar to the following:
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2011-06-17 11:10+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: volume/templates/volume/Volume2.html:8
#: volume/templates/volume/Volume2.html:18
msgid "Part"
msgstr ""

#: volume/templates/volume/Volume2.html:8
msgid "Three Toms and Rolls"
msgstr ""

#: volume/templates/volume/Volume2.html:9
msgid ""
"This section contains the first exercises containing snare drum rolls.  "
msgstr ""

#: volume/templates/volume/Volume2.html:18
msgid "Ride Cymbal and Cut Common"
msgstr ""

#: volume/templates/volume/Volume2.html:19
msgid ""
"This part introduces the ride cymbal to the drum kit."
msgstr ""

Add translation

Add your translation into the msgstr section (Warning! Google translate Norwegian ahead!)
#: volume/templates/volume/Volume2.html:8
msgid "Three Toms and Rolls"
msgstr "Tre Toms og Rolls"

#: volume/templates/volume/Volume2.html:9
msgid ""
"This section contains the first exercises containing snare drum rolls.  "
msgstr "Denne delen inneholder de første øvelsene inneholder skarptromme ruller."

#: volume/templates/volume/Volume2.html:18
msgid "Ride Cymbal and Cut Common"
msgstr "Ride Cymbal og Cut Common"

#: volume/templates/volume/Volume2.html:19
msgid ""
"This part introduces the ride cymbal to the drum kit."
msgstr "Denne delen introduserer ride cymbal til trommesett."

Compile Language File

Once we've put our translations in, we need to compile the .po file into a .mo file.
me@coderpriyu:~/web/progperc/site$ ~/web/progperc/django/bin/django-admin.py compilemessages
processing file django.po in /home/coderpriyu/web/site/locale/nb/LC_MESSAGES
You should now be able to run up your app, change your locale in the browser, and see the translated text.

3 comments:

  1. It's not working what should i do ?

    ReplyDelete
  2. language code is en-gb etc is working ?

    ReplyDelete
  3. Have u make sure that in your setting.py file USE_I18N is set to True ??

    ReplyDelete