Michael Gajda
Michael Gajda

Reputation: 105

Multilanguage Content in Rails

I'm about to start a new Project and need some advise.

For example, if i have a Model named "Page" that has "Posts" - how can i store more than one language when i create a new post and show only posts in a language when i click a - let's say - flag-icon at the top.

I have read a lot about l18n but as i understood - this is the way if i want to translate static messages like errors etc. ?

Hope somebody could explain a given strategy to to this in a clean way.

Thanks!

Upvotes: 1

Views: 521

Answers (1)

mnemosyn
mnemosyn

Reputation: 46281

Like you said, localization and internationalization (abbreviated l10n and i18n, respectively) typically refer to the localization of the software product itself, rather than the content.

There are different strategies on how to manage content in multiple languages, and it does depend a lot on what you want to achieve. Suppose you operate a multilingual blog. However, some content is not relevant to an international audience, so you don't want to supply an English version (assuming your not a native English speaker, but I guess the point is clear).

Now it seems to make sense to simply not display that blog post in the English version of the blog. Hence, I'd suggest

Post {
  "_id" : ObjectId('...'),
  "PostGroupId: ObjectId('...'),
  "Title" : "A Blog Post Title",
  "Text" : "<h1>Lorem ipsum</h1> lots of text",
  "Language" : "en",
  "Published" : // and so on...
}

You can now easily query for all or specific posts in a given language: db.Posts.find({"language" : "en"}).sort({"Published" : -1});

Depending on your needs, you might want to add a grouping object for the posts to associate translations of posts to each other explicitly, using denormalized data:

PostGroup
{
  "_id" : ObjectId('...'),
  // ...

  "Posts" : [{"lang" : "en", "id" : ObjectId('...')},
             {"lang" : "de", "id" : ObjectId('...')} ]
  // -- or simpler --
  "AvailableLanguages" : ["en", "it", "fr"]
}

Upvotes: 1

Related Questions