Multilingual By Choice pt.1
Friday, 31 Jul, 2009Django Multilingual is bliss. Especially for us, non-native english speaking devs, since we are the ones that usually have to support more than one language.
However, apps that are released for public usage, should not depend on DM, so people that do not actually care about more than one language, will not be forced to install it.
This is a first in a series of posts, that will walk you through on how we manage that here, at Phaethon Designs.
Let's we have a Blog app (how refreshing!):
from django.db import models
class Post(models.Model):
title = modelsCharField(max_length=80)
slug = models.SlugField()
body = models.TextField(blank=True)
But wait, we should be able to post in more than one language:
from django.db import models
import multilingual
class Post(models.Model):
class Translation(multilingual.Translation):
title = modelsCharField(max_length=80)
body = models.TextField(blank=True)
slug = models.SlugField()
Ok, you already knew that. How do we make it optional though? We add a conf/ directory
to our app base dir:
blog/
conf/
settings.py
__init__.py
and in it, a settings.py file:
from django.conf import settings
IS_MULTILINGUAL = getattr(settings, 'BLOG_IS_MULTILINGUAL', False)
then in our models.py we can do:
from django.db import models
from blog.conf import settings
if settings.IS_MULTILINGUAL:
import multilingual
class Post(models.Model):
if settings.IS_MULTILINGUAL:
class Translation(multilingual.Translation):
title = modelsCharField(max_length=80)
body = models.TextField(blank=True)
else:
title = modelsCharField(max_length=80)
body = models.TextField(blank=True)
slug = models.SlugField()
So here it is, our blog app now, can be multilingual on demand.
Go ahead and check Multilingual By Choice pt.2.
Comments
Looks like you're repeating yourself quite a lot there. :(
Somehow the "if" statement in the class definition doesn't feel right. I can not suggest an alternative yet though. I will ponder on this some more. Thanks for sharing your thoughts.
By the way, the indentation in the last "class Post" seems incorrect...
Cheers, Robert
how about writing the if in the settings.py when selecting the apps to include. if multi languages are not required simply include a transparent app.
@John+Robert: I'm not happy with the "if" statement either, but it was the only way I could think too.
@dude: not Exactly sure how we can achieve that, at the settings.py level.
Comments for this entry are closed. If you wish to further discuss this post, please contact us directly.