Macklin
Macklin

Reputation: 103

I18n translation of model data

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 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!

Upvotes: 1

Views: 1511

Answers (2)

Jiemurat
Jiemurat

Reputation: 1639

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

Upvotes: 0

edgerunner
edgerunner

Reputation: 14983

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]}

Upvotes: 2

Related Questions