I cannot figure out how to translate model data in Rails (3.0.1) with I18n.
Example Situation:
I have a form where a registrant selects their sport. The sport 开发者_StackOverflow中文版select is a dropdown populated by calling:
Sport.all.collect{ |s| [s.name, s.id]}
I'm tempted to naively write:
Sport.all.collect{ |s| [t(s.name), s.id]}
But I don't see how rails would know to search for names like "Track and Field". How would I provide the names in different languages? What are best practices for implementing this?
Thanks all!
If Sport
is a model in the database, you'd be better off keeping translations in the database as well. Add a language
column to your Sport model and populate with
Sport.where(:language => I18n.locale).collect{|s| [s.name, s.id]}
I think the most proper way is to use globalize3
If the globalize gem raises mass assignment error during db:migrate
, then add following code to your initializer:
Globalize::ActiveRecord::Translation.class_eval do
attr_accessible :locale
end
精彩评论