Translating Templates with Django
Below is steps for translating one Django websites into Norwegian.
This defaults to English, but limits the list of languages on offer. I also added
becomes
Add
This error means that we need to create a
This error occurs because we don't have the
Success!:
I now have a file, in
You should now be able to run up your app, change your locale in the browser, and see the translated text.
Settings
As I'm only providing translation for Norwegian, I set the following in my settings.py file:This defaults to English, but limits the list of languages on offer. I also added
LocaleMiddleware
to my MIDDLEWARE_CLASSES
:Mark Up Template
Open up the template to translate, and add the trans tag around the text you want translating. So,becomes
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 rundango-admin.py makemessages
in the same directory as your settings file, after creating a locale
directoryThis 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
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
~/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.You should now be able to run up your app, change your locale in the browser, and see the translated text.
It's not working what should i do ?
ReplyDeletelanguage code is en-gb etc is working ?
ReplyDeleteHave u make sure that in your setting.py file USE_I18N is set to True ??
ReplyDelete