简体   繁体   中英

ActiveRecord translate nested attribute

I've got a model that looks like this:

class Search
  validate :customers_are_of_age

  def customers
    [customer1, customer2]
  end

  def customers_are_of_age
    unless customers.all?(&:of_age)
      errors.add(:customers, :not_of_age)
    end
  end
end

It's a contrived example but highlights the issue I'm facing. Now my translation file looks like so:

en:
  activemodel:
    errors:
      models: 
        search: 
          customers:
            not_of_age: "not of age"
de:
  activemodel:
    errors:
      models: 
        search: 
          customers:
            not_of_age: "nicht im Alter"

This works mostly OK, in that if I have a search object which fails the validation, I get the message "Customers not of age" which is what I want. However in German, I get the message "Customers nicht im Alter".

How do I translate the "customers" key? I've tried the following to no avail:

activemodel:
    errors:
      attributes:
        search/customers: "Customers"
      models: 
        search: 
          customers:
            not_of_age: "not of age"

I also tried this, which doesn't work either:

activemodel:
      models: 
        search: 
          customers:
            one: "Customer"
            many: "Customers"
            not_of_age: "not of age"

the reason that you see "Customers nicht im Alter" is because the model name is not translated(for customer model). You only added translation for the error. You can find more here: http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models

de:
  activemodel/activerecord(depnding of what you use):
    models:
      customer:
        one: *german translation for one*
        other: *german translation for many*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM