开发者

Rails: translations for table's column

In rails application I have two models: Food and Drink.

Both food and drink have a name, which has to be stored in two languages.

How do I better 开发者_如何学运维realize translations for theese tables?

First solution I realized was to replace name column with name_en and name_ru.

Another solution is to encode with YAML hash like { :en => 'eng', :ru => 'rus' } and store yaml as a name.

What would you recommend, assuming content is not static?

Maybe there's good article?


The first option (name_en, name_ru) is the easiest to implement.

You could include a name method that returns the right value depending on the selected locale. You could even create a module, if you are going to use this on lots of models/fields:

class Food < ActiveRecord::Base
...
  def name
    self.send("name_#{I18n.locale}")
  end
end

If in the future you must include an additional language, you will have to add a migration, of course. But that shoudn't be too troublesome.

The second one (encoding using YAML) seems a bit more cumbersome - you will not have to make an additional migration with it, but you loose other functionality. For example, search is made much more difficult - you can't use SQL any more for looking through descriptions, as they are coded on YAML instead of being plain text.

So I recommend having two fields.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜