Multilingual fields in Django

A while ago, back when Django was in it's 1.3 version, my first serious project was http://datoz.com, a real estate information site featuring, among other things, bilingual descriptions of office spaces.

Back then I wrote about how we managed that in a quick and easy manner, with no external dependencies, essentially by creating separate fields for each language and unifying acces to them with a property.

from django.utils import translation
 
class Product:
description_en = models.CharField()
description_es = models.CharField()
@property
def description(self):
lang = translation.get_language()
return getattr(
self, 'description_%s' % lang,
_(u'Not available')
)

Sort of quick and dirty, yeah, but back then existing apps for doing that striked me as buggy, or too big, or outright unmantained. After sharing that post on Reddit people helpfully pointed me to a couple of libraries that do a much better job, namely:

You should check them out!

If you liked this article, say hello on Twitter

blog comments powered by Disqus