DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Multi-lingual Model In Django Used For Internationalizing Content

11.08.2006
| 7006 views |
  • submit to reddit
        The approach taken here gives each object (in this case a wiki Page) a language and a translation_of field.  These two fields can be added to any model that wants to add content translations.

Pages are created in a default language. To translate a page, you create a new page and set the translation_of field to the page in the default language that you are translating.

A set of helper methods allow you to get the root version of the page (in the default language), all translations of a page, or a specific translation. These are useful for adding translation links.

LANGUAGE_CHOICES = (
    ('en', 'English'),
    ('nl', 'Nederlands')
)

class Page(models.Model):
    language = models.CharField(maxlength=2, choices=LANGUAGE_CHOICES)
    title = models.CharField(maxlength=60)
    path = models.SlugField(prepopulate_from=('title',))
    translation_of = models.ForeignKey('self', related_name='translation_set', limit_choices_to = {'language' : settings.DEFAULT_LANGUAGE}, blank=True, null=True, help_text="Select which page this is a translation of. Don't set this for english pages.", validator_list = [local_models.validate_translation_of])
    content = models.TextField(blank=True)

    def get_root_translation(self):
        """Returns the root translation for this page.
        
        This page will be in the settings.DEFAULT_LANGUAGE."""
        if self.translation_of is None:
            return self
        else:
            return self.translation_of

    def get_translations(self):
        """Return all of the translations for this page."""
        # If I am the root translation, just return all of my translations.
        if self.translation_of is None:
            return self.translation_set.all()
        else:
            # If I am not the root, return the root translations, plus all of its
            # translations, minus myself.
            return Page.objects.filter(
                models.Q(id=self.translation_of.id) | 
                models.Q(translation_of=self.translation_of.id)).exclude(pk=self.id)
                
    def get_translation(self, language):
        """Return a specific translation of this page."""
        if self.language == language:
            return self
        # If I am the root translation, search for translations of me in the given language
        elif self.translation_of is None:
            try:
                return Page.objects.get(translation_of=self.id, language=language)
            except Page.DoesNotExist:
                return None
        # Find the root page and the specific translation
        else:
            # If am not the root, find the root page with the given language,
            # otherwise find the page that is a translation of me in the given language
            return Page.objects.filter(
                models.Q(id=self.translation_of.id) |
                models.Q(translation_of=self.translation_of.id),
                language=language)

In local_models.py there's this validator to check if the translation_of attribute was not set for pages in the default language:

def validate_translation_of(field_data, all_data):
  if field_data is not None and len(str(field_data)) > 0 and all_data['language'] == settings.DEFAULT_LANGUAGE:
    raise validators.ValidationError("Do not set the translation for english pages.")