开发者

Which model design is better?

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(开发者_JAVA技巧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?


The model that he proposes seems more sensible; treating the languages The same makes more sense to me than treating them differently, and it is more appropriately normalized.

However, rather than creating a table for each language, create one table for content and add a column to indicate the language.


Typically, languages are stored like this (pseudo-code):

posts (
  id       serial pkey,
  pubdate  datetime
)

posts_lang (
  id       int fkey posts (id),
  lang     char(2),
  title    varchar,
  content  text,
  pkey (id, lang)
)

An alternative approach I've seen a few times, is this:

posts (
  id       serial pkey,
  parent   int fkey posts (id),
  lang     char(2),
  pubdate  datetime
  title    varchar,
  content  text
)

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:

posts (
  id       serial pkey,
  pubdate  datetime
  title    varchar,
  content  text,
)

posts_lang (
  id       int fkey posts (id),
  lang     char(2),
  title    varchar,
  content  text,
  pkey (id, lang)
)

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:

en.posts (
  id       serial pkey,
  pubdate  datetime
  title    varchar,
  content  text
)

es.posts (
  id       serial pkey,
  pubdate  datetime
  title    varchar,
  content  text
)

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜