Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle translations for an ActiveModel?

I am using Rails 3.1.1 and I would like to properly translate error messages for an ActiveModel. I don't know if overwriting the i18n_scope is the right way to solve my problem (or if there are other ways), but the official documentation says:

i18n_scope()

Returns the i18n_scope for the class. Overwrite if you want custom lookup.

... how should I overwtite the i18n_scope?

At this time I am getting a the following "alert":

# Note the 'activemodel' part
translation missing: de.activemodel.errors.models.my_class.attributes.message.blank

# I would like to "map" translations to 'de.activerecord.errors.messages.blank'
# as made for all other ActiveRecord classes in my application

My ActiveModel class is like the following:

class MyClass
  include ActiveModel::Conversion
  include ActiveModel::Validations
  include ActiveModel::Dirty
  extend  ActiveModel::Naming
  extend  ActiveModel::Translation

  validates :name, :presence => true

  ...
end
like image 798
Backo Avatar asked Jan 12 '12 12:01

Backo


1 Answers

It should be a class method, by analogy with AR code:

class MyClass
  include ActiveModel ...
  class << self
    def i18n_scope
      :activerecord
    end
  end
end
like image 175
clyfe Avatar answered Oct 15 '22 15:10

clyfe