Rails 4:非 ActiveRecord 对象上的 ActiveRecord 样式 I18n?
Rails 4: ActiveRecord-style I18n on a non-ActiveRecord object?
我有一个 Plain Old Ruby Class (PORO),它的行为很像 ActiveRecord 模型,尽管它没有持久保存到数据库中。
为了尽可能轻松地实现国际化(I18n),我还想在此 PORO 上使用 SomeModel.model_name.human
和 SomeModel.human_attribute_name(:attribute)
方法。
我需要包含什么模块才能在我的 PORO 中包含上述方法?
使用 ActiveModel::Translation
模块扩展您的 class:
class Widget
extend ActiveModel::Translation
end
Widget.model_name.human
=> "Widget"
Widget.human_attribute_name :my_attribute
=> "My attribute"
我有一个 Plain Old Ruby Class (PORO),它的行为很像 ActiveRecord 模型,尽管它没有持久保存到数据库中。
为了尽可能轻松地实现国际化(I18n),我还想在此 PORO 上使用 SomeModel.model_name.human
和 SomeModel.human_attribute_name(:attribute)
方法。
我需要包含什么模块才能在我的 PORO 中包含上述方法?
使用 ActiveModel::Translation
模块扩展您的 class:
class Widget
extend ActiveModel::Translation
end
Widget.model_name.human
=> "Widget"
Widget.human_attribute_name :my_attribute
=> "My attribute"