You should create an initializer in which you'd put:
class ActiveRecord::Base
def self.has_translation(*attributes)
attributes.each do |attribute|
define_method "#{attribute}" do
self.send "#{attribute}_#{I18n.locale}"
end
end
end
end
Then in your models:
has_translation :name, :whatever
So that it will automatically call the proper column name depending on your current locale. In your view:
@variable.name # calling @variable.name_en if locale = :en
# @variable.name_es if locale = :es
Of course, I assumed there was no already existing table named name
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…