Skip to content

Instantly share code, notes, and snippets.

@ojii
Created October 7, 2010 12:33
Show Gist options
  • Save ojii/615028 to your computer and use it in GitHub Desktop.
Save ojii/615028 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from django.db import models
from translateable.models import TranslatedModel
class MyModel(TranslatedModel):
non_translated_field = models.IntegerField()
class Translations(models.Model):
translateable_field = models.IntegerField()
class TranslationsMeta:
related_name = 'translations'
fallback_method_name = 'get_fallbacks'
"""
instance.translateable_field
=> current translation OR NONE!
instance.translations
=> reverse FK from MyModel.Translations
instance.get_fallbacks('de', 'fr', 'ja')
=> populates translatable properties on 'instance' which are NOT filled
with the values from the languages requested, in that order.
EG: we have fields 'one', 'two', 'three' and 'four'.
current language is 'de'.
German translation:
one: 'eins'
two: None
three: None
four: None
English Translation:
one: 'one'
two: 'two'
three: None
four: None
Japanese Translation:
one: '一',
two: '二',
three: '三',
four: '四',
instance = Model.objects.get(pk=something)
instance.one => 'eins'
instance.two => None
instance.three => None
instance.four => None
instance.get_fallbacks('en', 'ja')
instance.one => 'eins'
instance.two => 'two'
instance.three => '三'
instance.four => '四'
MyModel.objects.all().with_fallbacks('de', 'ja')
=> FROZENQuerySet which does 2 queries: one for the objects, one for
fallbacks, does the 'fallback-setting' in python in iterator
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment