Jump to content

RapidSMS Developers Guide/Internationalization

From Wikibooks, open books for an open world

This section explains how to localize your application. This describes how switch from one language (code language, usually English) to another language. If you want to serve multiple languages at the same this, do it yourself.

Changing your code to support Localization

[edit | edit source]

Create a new directory inside your app and name it locale. In our case: we create apps/survey/locale

Import needed packages to translate our text in each file which contains text strings or messages to be translated.

In app.py and models.py we have to import the gettext translation lib from django and from it we need the ugettext function.:

from django.utils.translation import ugettext as _

We give an alias for ugettext: _ to make the code more clear and easier to write.

Note: it is recommended in models and GUI to import ugettext_lazy function instead of ugettext

For each message you want to translate, make a call to the _() function, passing it the text. For instance :

message.respond(u"Hello World!")
message.respond(_(u"Hello World !"))

To translate a message with parameters, make sure you only translate the format, not the evaluated string :

message.respond(u"Thank you %s. for your opinion" % lname)
message.respond(_(u"Thank you %s. for your opinion") % lname)

If Django finds this sentence in the file django.mo file (See bellow) it will retrieve the corresponding translation otherwise it will take the default text which is the original one.

Creating the translation file

[edit | edit source]

Once you have marked all your strings as translatable in your app, create a translation file using the following command:

cd apps/myapp
mkdir locale
django-admin.py makemessages –l ar 

In the previous example, myapp is the name of the app you are translating and ar is the language code of your target language.

In the locale folder Django will create a new folder named after the language code (ar here) with an LC_MESSAGES subfolder which itself contains a file named django.po.

This file is the file to be translated. That's the one you can share with a translator.

Each time you create or modify message new translatable strings in your app, re-issue the makemessages command to to update .po file.

Translation

[edit | edit source]

Open django.po file and translate your text for each message like:

msgid "Thank you %s. for your opinion”
msgstr "شكرا لك %s. لمشاركتنا رأيك"


  • msgid: the original message “default language”
  • msgstr: the corresponding translation.

Note: It is mandatory to keep all formating parameters (%s, %d, %(key)s, etc) from the original to the translation. For Arabic translation, %s must appear like this, not s%.

Compiling the translation

[edit | edit source]

Django doesn't use the dango.po file. It uses a compiled version of it: django.mo. To compile your .po file, use the following command:

django-admin.py compilemessages

This command will create .mo file which is the one used by Django and rapidsms to display localized strings.

Relaunch the command everytime you update the translation .po file to see the changes.

Enabling localized version

[edit | edit source]

To be able to use your localized version, your need to configure RapidSMS to use that language.

This is done using the bonjour app. Link it if you haven't then add it to your local.ini apps= list.

Lastly, you need to configure bonjour in local.ini to specify the language code:

[bonjour]
lang=ar


Git and the Community · Coding standards and documentation