【问题标题】:ActiveRecord translate nested attributeActiveRecord 翻译嵌套属性
【发布时间】:2018-01-16 18:33:30
【问题描述】:

我有一个看起来像这样的模型:

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

这是一个人为的例子,但突出了我面临的问题。现在我的翻译文件是这样的:

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

这几乎可以正常工作,因为如果我有一个未通过验证的搜索对象,我会收到我想要的消息“未达到年龄的客户”。但是在德语中,我收到消息“Customers nicht im Alter”。

如何翻译“客户”键?我尝试了以下方法无济于事:

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

这个我也试过了,还是不行:

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

【问题讨论】:

  • activemodel: errors: models: search: customers: "xxxxx" 怎么样?这似乎与您的工作结构一致。您向客户展示的两个想法似乎与它不一致。

标签: ruby-on-rails ruby activerecord internationalization


【解决方案1】:

您看到“Customers nicht im Alter”的原因是型号名称未翻译(针对客户型号)。您只为错误添加了翻译。 你可以在这里找到更多: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*

【讨论】:

    猜你喜欢
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-11
    • 1970-01-01
    相关资源
    最近更新 更多