ruby-on-rails – 如何处理ActiveModel的翻译?

我正在使用Rails 3.1.1,我想正确翻译ActiveModel的错误消息.我不知道如果覆盖i18n_scope是解决我的问题的正确方法(或者有其他方法),但是 official documentation说:

i18n_scope()

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

…我应该如何超越i18n_scope?

在这个时候我得到以下“警报”:

# 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

我的ActiveModel类如下所示:

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

  validates :name,:presence => true

  ...
end

解决方法

它应该是一个类的方法,类比与 AR code
class MyClass
  include ActiveModel ...
  class << self
    def i18n_scope
      :activerecord
    end
  end
end

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...