i18n and l10n

translating python web apps

Posted by on April 01, 2022 · 3 mins read

Ever since my Machinaris cryptocurrency platform launched a year ago, the community that sprung up around it has been very active. Drawn from around the world, they have provided invaluable feedback and guidance on the direction of the platform as it has matured and become more powerful.

However, as a native English speaker, Machinaris has so far only been available in English. This changed with the most recent release of Machinaris version 0.7.0. With great contributions from community members, Machinaris has been both internationalized and localized.

Translations

The first step was to extract all the hard-coded English messages throughout the application. I selected Flask-Babel for this. So basically, where a English string in the web template was before:

    This is an English message.

This was extracted using Flax-Babel, by wrapping it in a convenience translation method:

    { { _('This is an English message.') } }

Running the Babel language pack initialization for supported locales, then the extraction, resulted in messages.po files such as this one with an msgid (English key) corresponding to the msgstr (local translation):

Each browser request sends an ‘Accept-Language’ weighted list of accepted languages. Here’s the settings from Mozilla Firefox:

Machinaris will find the best match between the browser’s requested language and the supported locales. The fallback default is en (English). Here’s what this looks like in Portuguese:

Number Formats

Machinaris uses Babel to support locale-dependent number and currency formatting. For example, this allows for decimals to use commas as a grouping-separator in the US, while a comma represents the decimal point in France.

Date Formats

There is only one true date format: ISO8601. Seriously though, this allows easy sorting in tables by date column.

Conclusions

Overall, the Babel library is the definitely the way to go when internationalizing a application in Python. Combined with the Flask web layer, it allows one to release a multi-lingual application for users all around the world.

More in this series…