Tuesday, April 29, 2008

Django L10n

This Sunday, I participated in This Week in Django, and tried to give some ideas on Django localization.

Here I'll post some of the ideas of the interview (and some that I missed), for serving as reference:

How to translate your application (quick guide):

  • Mark every text in your application for translation:

    • In models.py, views.py... convert 'my text in just one language' to _('my text to translate'). Don't forget to import _: from django.utils.translation import ugettext_lazy as _

    • In templates, convert <p>Text in english</p> to <p>{% trans 'Text in many languages' %}</p> (also this can be done with blocktrans tag)

  • Go to your project path and create a directory called locale (also you can do that just for an application)

  • Execute ${PATH_TO_DJANGO}/bin/make-messages -l ${LANGUAGE_CODE} (where language code is en for english, es for spanish...)

  • Edit ${PROJECT_PATH}/locale/${LANGUAGE_CODE}/LC_MESSAGES/django.po and set the msgstr variables with the translation of every msgid

  • Run msgfmt django.po -o django.mo (I just realized after the interview that exists a django script complie-messages.py that does that for all .po files)

  • And then you have your application translated. There are some settings in settings.py that need to be set for making it work (USE_I18N = True, set LANGUAGES and LANGUAGE_CODE, and specify the django.middleware.locale.LocaleMiddleware middleware)

  • Then probably you'll want to have your select input with all available languages (or something like that). For it you'll have to add (r'^i18n/', include('django.conf.urls.i18n')) to your urls.py, and from your html send a POST request to /i18n/setlang with the parameter language set to desired language code

  • For more stuff, and detailed information check: http://www.djangoproject.com/documentation/i18n/

Things that IMHO should be improved in Django for a better L10n expirience:

  • Move localflavors outside trunk (to avoid unnecessary translation costs). Every localflavor should come with necessary translations.

  • Create locale settings (besides translations), to set decimal symbol, date and time format, first day of week... and use it automatically for current locale/language.

    • Check tickets #1061, #3940 and #6783 that gives different approaches to this problem.

  • Create translatable CharFields and TextFields. For now django-multilingual and transdb can be used for it.

  • Adding something to select the language in admin (when more than one is available).

  • Haven't checked it too much, but it'll be good if urls could be translated as well.

Finally I want to thank for letting me participate in TWID to Michael Trier, who is a father, husband, software architect, entrepreneur, a great journalist, and a better person. And also to Malcolm Tredinnick, who recommended me to the show (not sure if I deserved the honour), and for his unpayable help and support on my Django work.


  1. I have rather good experience with using Babel (http://babel.edgewall.org/) for l10n in my Django-based projects. While not yet complete (and has some bugs, be sure to check outstanding tickets in their trac), it simplifies many things, like formatting dates or currencies. This makes regionalized format settings unnecessary.

  2. Hi Marc !!!

    congratulations for your interview in twid !!! Hope you enjoyed it ;)

    cheers, r

  3. If you use format strings (e.g., 'The %s is %s. % (a, b)) with more than one interpolated variable in your model or view code, you probably want to use dict interpolation:


    Most of the cases where you'd think of using this you should actually be using a template, though.

    btw, the example is rather bad: "The" in English becomes, e.g., 'el' or 'la' in Spanish depending on the gender of the parameter. Your Spanish translator won't be so happy just seeing that bare string; do him/her a favor and inform them about how the string is going to be used and what values will get filled in. You can even include that information in the English string, then provide an English "translation" that removes it.

  4. thank you for sharing Congratulations on a very beautiful designed site http://www.bayraksatisi.com