As we discussed in the design decisions one key challenge for our model design is the question of how language interacts with our concept of an object: do we have abstract entities with associated translations, text which only exists in one language or some combination of the two? Do I support only a small number of pre-defined languages or anything which someone on the internet felt like contributing?
City
has a country and and a
GeoDjango shape) and uses related objects for
translation
Any of these could be correct depending on the project. We'll look at two Django apps which help manage the second and third possibilities
title
field will become title_en
,
title_de
, etc.
obj.title
becomes a property which returns
title_LANGUAGE_CODE
.
This is avoids adding any extra tables or performing extra queries but it will retrieve considerable extra data if you have more than a few languages or very much text. You can compensate partially using Django's defer(), only values or values_list queryset methods to retrieve only the fields you care about at the cost of having to code some language-specific queries.
TranslatableModel
class which your
models should subclass. Untranslated fields are declared
normally; translated fields are declared using a
TranslatedFields
helper
TranslationQueryset
provides a
language()
method which will automatically
populate the fields of a retrieved object with the desired
language, optionally falling back to other languages when
a particular translation doesn't exist
hvad retrieves only the translations you care about and allows you to avoid duplicating the untranslated portion of your model data. On the downside, every query now involves at least an extra JOIN which may be a performance concern, particularly for MySQL users.
/
#