Skip to content

Instantly share code, notes, and snippets.

@stefanw
Created May 25, 2010 00:13
Show Gist options
  • Save stefanw/412589 to your computer and use it in GitHub Desktop.
Save stefanw/412589 to your computer and use it in GitHub Desktop.
# From my lightning talk at DjangCon.eu 2010
# I18N for User Content: Wikipedia Style
# Approach by django-modeltranslation
class News(models.Model):
title = models.CharField(max_length=255)
title_de = models.CharField(null=True, blank=True, max_length=255)
title_en = models.CharField(null=True, blank=True, max_length=255)
text = models.TextField()
text_de = models.TextField(null=True, blank=True)
text_en = models.TextField(null=True, blank=True)
# and transmeta:
from transmeta import TransMeta
class Book(models.Model):
__metaclass__ = TransMeta
title = models.CharField(max_length=200)
description = models.TextField()
class Meta:
translate = ('title', 'description',)
# basically the same, just different tools and trickery
# Approach by transdb
class MyModel(models.Model):
my_char_field = TransCharField(max_length=32)
my_text_field = TransTextField()
# ...
u'{u'en': u'This is english', u'ca': u'Això és català'}'
# Approach by django-pluggable-model-i18n
class Item(models.Model):
title = models.CharField(max_length=150)
class ItemTranslation(translator.ModelTranslation):
fields = ('title',)
translator.register(Item, ItemTranslation)
# generates:
class ItemTranslation(models.Model):
_language = models.CharField(db_index=True)
_master = models.ForeignKey(Item, related_name='translations')
title = models.CharField(max_length=150)
# We used the model inheritance to connect objects with sites:
from django.db import models
from django.contrib.sites.models import Site
class SiteModel(models.Model):
site = models.ForeignKey(Site)
def save(self, **kwargs):
if self.site_id is None:
self.site = Site.objects.get_current()
super(SiteModel, self).save(**kwargs)
class News(SiteModel):
title = models.CharField(max_length=255)
# A manager factory creates two managers from two different superclasses
from django.db import models
from django.contrib.sites.managers import CurrentSiteManager
def get_custom_news_manager(superclass):
class GenericNewsManager(superclass):
# custom methods here
return GenericNewsManager
class News(SiteModel):
title = models.CharField(max_length=255)
# ...
anysite_objects = get_custom_news_manager(models.Manager)()
objects = get_custom_news_manager(CurrentSiteManager)()
# a site_settings.py is imported with site specific settings
LOCALE = "de.UTF-8"
LANGUAGE_CODE = 'de'
SITE_ID = 2
SITE_DOMAIN = "de.bpmn-community.org"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment