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

前端之家收集整理的这篇文章主要介绍了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
原文链接:https://www.f2er.com/ruby/272760.html

猜你在找的Ruby相关文章