尝试使用Rails I18n覆盖ActiveRecord字段和模型名称

时间:2018-01-23 20:09:34

标签: ruby-on-rails ruby activerecord internationalization

我正在使用Rails 5.2.1,并试图覆盖一些ActiveRecord模型和字段的名称。

我知道Rails正在选择英语区域设置文件,因为当我将以下内容添加到locales/en.yml时,我的表单标签会更新为正确的值:

en:
  activerecord:
    models:
      seller_address: "Address"
    attributes:
      seller_address:
        name: "Name"
        street1: "Street Line 1"
        postal_code: "Postal Code"

但是当我致电@seller_address.errors.full_messages时,我仍然看到旧的字段和模型值。例如,这个:

Seller addresses name can't be blank

Seller addresses street1 can't be blank

Seller addresses postal code can't be blank

应该是这样的:

Addresses Name can't be blank

Addresses Street Line 1 can't be blank

Addresses Postal Code can't be blank

我该怎么做才能告诉Rails使用我的语言环境覆盖ActiveRecord验证错误消息?

1 个答案:

答案 0 :(得分:0)

您需要在errors.en.yml文件中定义这样的i18n错误。

en:
  activerecord:
      attributes:
      errors:
        full_messages:
          format: "%{attribute} %{message}"         # You can change your error format here.
        models:
        seller_address:
          attributes:
            name:
              blank: 'Addresses Name can't be blank.'
            street1:
              blank: 'Addresses Street Line 1 can't be blank.'