We are now using Django to develop a multilingual website. We have different language version for content on our website. For example, for a post, we have English and Spanish version.
Currently we are using this model:
class Post(models.Model):
user
title
detail
count_follower
...
orginal_language
date
class PostEspanish(models.Model):
post = models.ForeignKey(Post)
title
detail
So we use Post for all English content and PostEspanish for Spanish content.
If someone writes a post in English we just put it in Post model, and we put translated post in PostEspanish model. And if someone writes a post in Spanish, we first create a Post instance, then create a PostEspanish instance and put content in PostEspanish, and if someone translate this Spanish post, we put translated post in its referring Post.
Storing different language content on different model is because the search guy want it this way. He says it is good for the search.
To make it clearer, for instance, some user writes a post, we will do the following:
if language == 'English':
post = Post.objects.create(..., orginal_language='english')
else:
post = Post.objects.create(..., original_language='espanish')
PostEspanish.objects.create(post=post, ...)
and translates:
post = Post.objects.get(id=id)
if post.orginal_language == 'english':
post = post.postespanish
#update post
post.save()
else:
#update post
post.save()
Today someone said this model design is really poor. He said the current model is not well object oriented. Here is the way they do it:
class Post(models.Model):
user
count_follower
...
orginal_language
date
class PostContentEnglish(models.Model):
post = models.ForeignKey(Post)
title
detail
class PostContentEspanish(models.Model):
post = models.ForeignKey(Post)
title
detail
So the code will be like this:
if language == 'English':
post = Post.objects.create(..., orginal_language='english')
PostContentEnglish.objects.create(post=post,...)
else:
post = Post.objects.create(..., orginal_language='espanish')
PostContentEspanish.objects.create(post=post,...)
But most of us thought there is no difference between these two approaches. And his model design will generate one more table which is really bad.
So which one do you think is better?
Typically, languages are stored like this (pseudo-code):
An alternative approach I’ve seen a few times, is this:
Its benefit is that it allows to deal with locale-specific attributes (english tags/meta vs spanish tags/meta, for instance). But it quickly turns into a nightmare of tree handling. So not recommended.
And, of course, there’s the one you’re using, with the default language directly in the table:
I can see the rational for wanting the default language straight into the table. But in my experience it introduces code duplication — i.e. you’re constantly doing things two ways — and thus bugs/quirks. So can’t really recommended either.
My preferred alternative is none of the above — and using a different schema (or DB, or table prefix) for each language:
The reason I prefer it is that it’s frequent for a site to have untranslated pages and such, or pages that exist on one site but not the other. And more often than not your content should not be identical from a country to the next anyway — you do not communicate to your audience the same way.