Last Updated: February 25, 2016
· jairtrejo

Quick & dirty multilingual fields in Django

If you work a multilingual site you probably have fields in your models that you need to offer in several languages. Product descriptions, for instance, should be captured in every language and displayed in the current user's one.

What I do is define description_en, description_es, description_fr, etc. fields on my model, and then write a property that automagically picks the right one:

from django.utils import translation

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

Which allows me to do {{ product.description }} in my templates and have everything just work.

2 Responses
Add your response


+1 for using the word "automagically" ;-)

over 1 year ago ·

this is basically what this package does :

over 1 year ago ·