我正在使用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验证错误消息?
答案 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.'