How to translate a website based on Django ?


A quick guide on how to translate a website based on Django:

Edit the file settings.py

Let's assume that we want our django website available in English and French. First step, let's edit the settings.py file by adding the following lines:

from django.utils.translation import ugettext_lazy as _

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

LANGUAGES = (
    ('en-us', _('English')),
    ('fr', _('French')),
)

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'),
)

here by default the site is in english (LANGUAGE_CODE = 'en-us').

Create locale folder

Next step lets create a folder named "locale" at the root of the django project:

mkdir locale

so it should look like this:

locale
myproject/
    settings.py
    urls.py
myapps/
    templates/
    models.py
    views.py

Edit template files

Next, in all templates files (for example home.html) change the text that you want to be translated to

{% trans 'text' %}

for examples:

{% trans 'Hello everyone !' %}

or

{% trans 'search' %}

Edit the file views.py

In the views.py file add the following line:

 from django.utils.translation import gettext

and then change all text that you want to be translated to

text = gettext("text")

for example with an error message:

err_msg = gettext("Oups somehting went wrong")

Create translation files

Now we can start to create the transaltion files that will be stored in the local folder than we created just above. For that it is necesary to install gettext. The installation depends on your current system, for example on mac (see How to install gettext on MacOS X) with homebrew:

brew unlink gettext && brew link --force gettext

To check where gettext has been installed (for example /usr/local/Cellar/gettext/0.21):

brew info gettext

then enter:

PATH="/usr/local/Cellar/gettext/0.21/bin:$PATH"

to avoid the following error when you wiill try to compile the messages (see django i18n doesn't work in mac):

CommandError: errors happened while running msguniq
msguniq: Cannot convert from "ASCII" to "UTF-8". msguniq relies on iconv(). This version was built without iconv().

Now in your terminal go to the root of your project:

/locale
/myproject
/myapps

and enter the following commands :

django-admin.py makemessages -l fr
django-admin compilemessages

It should then create the files in the folder locale:

django.po
django.mo

Edit the .po file

Then edit the .po file to translate the text.

Note: to edit the .po file there are some text editor such as poedit.

Change the language

Two options (set language within a django view, Explicitly setting the active language, Can't change language in django):

(1) in the settings.py file change:

LANGUAGE_CODE = 'en-us'

to

LANGUAGE_CODE = 'fr'

(2) in views.py add the following line

from django.utils import translation

you can now change from french

language = 'fr' 
translation.activate(language)

or english in a django view

language = 'en-us' 
translation.activate(language)

Note: can be useful if you want to let the visitor pick up their preferred language. Create a link:

/myapps/?language=fr

and in views.py

if 'language' in request.GET and request.GET['language']:
    language = request.GET['language']
    if language in ['fr','en']:
        if language == 'fr': 
            translation.activate(language)
        if language == 'en': 
            translation.activate(language)

References