Django L10n

Follow me for more content or contact for work opportunities:
Twitter / LinkedIn

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

      Text in english

      to

      {% trans 'Text in many languages' %}

      (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.

    Follow me for more content or contact for work opportunities:
    Twitter / LinkedIn