PyGrunn: django localization (‘l10n’) - Cees van Wieringen

Tags: pygrunn, django

(One of my summaries of a talk at the 2017 PyGrunn conference).

What is L10N (=LocalizatioN)? You have multiple terms like that.

  • Translation (t9n): just translating text.

  • Localization: the adaption of a product or content to a specific language or culture. This includes localization. But also date/time formats. Currency. Units of measurement. Number formatting.

  • i18n (internationalization) is preparing your product for localization. Using unicode. Django translation toolkit.

Translation in django. Enable it in your by adding I18N = True. And use _() around your strings.

Where does the underscore function come from? It is a gettext function:

from django.utils.translation import gettext as _

There are multiple gettext variants (gettext_lazy, for instance), so he thinks it best to use the full function name instead of the customary underscore.

Sometimes you have terms that can be translated in multiple ways. Django has a gettext function that allows adding a “domain”. “Spring” can be a “season”, but it can also be a “mechanical part”.

For translating models, there are multiple django apps that handle it and that store the various translations in the database. But there is none that tries to grab the translation from the existing gettext *.po translation files. So he build one himself (“TransField”).

Localization trick for lengths: use Distance from the contrib.gis package. You can store a length as “300m” and then ask for and distance.yard(). They added similar classes for volumes and temperatures and areas and masses.

Then they build model fields like AreaField and MassField. For localization you can provide defaults (“I want my lengths in km and my weights in kg”). When outputting values, the correct localization and unit are applied automatically.

The code is at

A recommended video (8 minutes) that shows all the problems you might encounter when translating:

Photo explanation: just a nice unrelated picture from the my work-in-progress german model railway

Dutch note: python+django programmeren in hartje Utrecht bij de oude gracht? Watersector, dus veel data en geo. Leuk! Nelen&Schuurmans is op zoek. Stuur mij maar een mailtje, want de vacaturetekst staat nog niet online :-) logo

About me

My name is Reinout van Rees and I work a lot with Python (programming language) and Django (website framework). I live in The Netherlands and I'm happily married to Annie van Rees-Kooiman.

Weblog feeds

Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):