Ruby on rails 如何向活动模型添加翻译

Ruby on rails 如何向活动模型添加翻译,ruby-on-rails,rails-i18n,Ruby On Rails,Rails I18n,我有这个模型: class Coupon include ActiveModel::Validations include ActiveModel::Conversion extend ActiveModel::Naming attr_accessor :id def initialize(attributes = {}) attributes.each do |name, value| send("#{name}=", value) end

我有这个模型:

class Coupon
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  attr_accessor :id

  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end
  end

  def persisted?
    false
  end
end
如何将i18n翻译添加到此模型?具体而言:

  • 如何翻译其型号名称
  • 如何转换其属性
  • 如何为其提交按钮提供自定义翻译

我正在使用SimpleForm。

在您的config/locales文件夹中,创建一个activerecord.nl.yml(例如,对于Dutch),然后在该文件中放入活动记录翻译,如下所示

nl:
  activerecord: 
    models: 
      coupon: translation
      coupons: translation
对于标签和按钮,创建一个simple-form.nl.yml文件,其中包含

nl:
  buttons: &buttons
    submit: translation
    ...

在该文件的末尾,将

  simple_form:
    buttons:
      defaults:
        <<: *buttons
    labels:
      defaults:
        <<: *labels
simple\u表单:
按钮:
默认值:

很好的技巧,使用变量。尽管如此,它似乎并不适合我。属性和模型名称均未转换。这里还有测试代码,请注意,我只需要一个模型的自定义提交翻译,而不是所有模型的自定义提交翻译?简单的表单标签部分负责标签的翻译。对于按钮,我通常使用类似于=f.button:submit,t('buttons.submit')的东西,嗯,我认为为模型属性指定翻译将足以让simple_form确定它应该默认为标签的翻译。当我使用Mongoid时,它确实是这样工作的。在Mongoid中,我可以翻译Mongoid.attributes.book_keeping_entry.balance,Simpleform将使用它作为标签。请看它还声明“此外,当找不到标签的其他翻译时,Simple Form将从Rails返回默认的human_attribute_name。”我想您是指这个吗?我从来没用过
  simple_form:
    buttons:
      defaults:
        <<: *buttons
    labels:
      defaults:
        <<: *labels