Testing i18n in Django for the Molly Project

Published on

This was originally published on the Mobile Oxford Tech blog

Here in the Mobile Oxford team, we’ve been working with the team at Bangor University to help get the Molly Project, the open source project which grew from Mobile Oxford’s code base, ready to be translated into multiple languages (in Bangor’s case – Welsh!).

As Molly is a Django project, we can use Django’s extensive i18n framework to do most of the work for us, however, this does rely on us marking up the text for translation. When it comes to testing i18n, we needed a quick way of generating a test language file for us to drop in to check we’ve caught all of the strings that need to be translated.

Inspired by Richard Mitchell at Isotoma, who suggested using upside down English to test for Unicode bugs, we decided that testing i18n using upside-down English makes the most sense for us. And so, I wrote a script which did this. It ignores formatting strings and inside HTML, but everything else gets inverted using the lookup table defined at the top. We used this as we marked up each app in a feedback loop to make sure everything is marked up, and I think we got everything.

Data from external services (e.g., the library) isn’t translated, but where our data sources (e.g., the OpenStreetMap planet.osm dumps and the NaPTAN) do contain multi-lingual data, this is now imported into our database and tagged as the correct language, and where the data exists, it means you’ll see it in different languages!

However, we did find a few gotchas with Django’s i18n. First of all, there’s a fairly major bug in Django’s makemessages function, which means that the output of makemessages for JavaScript is unreliable as to whether or not we caught all the strings. The only real alternative is to generate a .po file by hand, however we cheated by creating a ‘dummy.js’ file which manually contains all the JS scripts, to make sure they’re found. However, this does mean we have to update this file every time we add a new string, which is a pain.

We’ve no plans to launch multiple languages on Mobile Oxford at the moment, but this is perhaps something we can look at in the future – translating the site into multiple languages to make it more useful for tourists to the city!