Django/Babel Integration

12 September 2007
00:02

This might have gone lost, either due to lack of interest, or due to myself having been flagged by some magical and over-ambitious “troll” filter on the django-developers mailing list. Anyway…

I recently released a project that provides integration between Django and Babel. This improves the support for internationalization and localization in Django quite a bit, in my humble opinion. The project is based on work by Ramiro Morales and Massimo Scamarcia, so kudos to them.

First, it provides a template tag library for using the Babel date & number formatting in Django templates. If you ever wondered how to get localized number formatting to work in your Django application, this is for you. Or more comprehensive localized date/time formatting. Or rendering decimals with currency symbols (as in, you know, prices). Etc.

Second, you can use the Babel message extraction framework with Django projects, including both Python code (obviously) and Django templates. Considering that the builtin message extraction for Django templates only works when you store your templates inside your projects' package directory, and only when the template files have the .html extension, using the Babel extraction routines can be quite liberating (I've had to use the .html extension on plain text email templates in the past, just to get the friggin extraction to work). Plus there's no dependency on the GNU gettext tools: if you have Python and Babel, you're ready to go. We don't have extraction from Javascript files yet, but I'd really like to add that.

Note that this does not replace the tried and tested gettext infrastructure. Somehow people tend to get that impression, probably needs to be clarified somewhere. What Babel does is build on top of gettext for message catalogs. The only part that it actually does replace is some of the most common command line tools, such as xgettext for message extraction or msgmerge for updating catalogs.

There's also a piece of middleware that adds the Babel Locale object to the request object. And for the future, I'd want to add django.newforms fields that parse and format date/time and number data in a locale-sensitive way. As soon as Babel properly supports date/number parsing, that is.