Rails: translations for table's column
In rails application I have two models: Food
and Drink
.
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
.
{ :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.
精彩评论