将 Rails 验证消息移至本地化 en.yml

问题描述 投票:0回答:1

我看到 Rubocop 出现 linting 错误,但我不太确定如何修复:

示例取自 Rubocop for Rails/i18nLocaleTexts

# bad
class User < ApplicationRecord
  validates :email, presence: { message: "must be present" }
end

# good
# config/locales/en.yml
# en:
#   activerecord:
#     errors:
#       models:
#         user:
#           blank: "must be present"

class User < ApplicationRecord
  validates :email, presence: true
end

但是,如果错误是在

Department
模型中的唯一性验证上发生的,例如:

# app/models/department.rb
...
validates :role, uniqueness: { scope: :company, message: 'admin is already present' }, if: -> { role_admin? }

尝试过:

# en.yml
 activerecord:
    errors:
      models:
        publication:
          attributes:
            base:
              must_be_in_future: Release date must be in future
        department:
          uniqueness: admin is already present
        ...

...然后如何重写上面的

validates :role, uniqueness: ...
语句以包含本地化文本?

ruby-on-rails validation internationalization
1个回答
2
投票

正如 Les Nightingill 在评论中提到的,正确的键是

taken
,而不是
uniqueness
。 如果您仔细选择,您通常可以避免为每个模型发送消息,而只需对所有模型使用相同的消息。这是我的做法:

#config/locales/en.yml
en:
  errors:
    format: "%{message}"
    header: 'The form entry has errors; please correct the errors below and re-submit.'
    messages:
      accepted:                 "The %{attribute} was not accepted; please accept the %{attribute}."
      blank:                    "The provided %{attribute} is blank; please enter a non-blank %{attribute}."
      confirmation:             "The provided %{attribute} does not match the corresponding entry; please re-check this entry against the original."
      empty:                    "The provided %{attribute} is empty; please enter a non-empty %{attribute}."
      equal_to:                 "The provided %{attribute} is incorrect; please enter exactly %{count}."
      even:                     "The provided %{attribute} is odd; please enter an even %{attribute}."
      exclusion:                "The provided %{attribute} is reserved; please enter a different %{attribute}."
      greater_than:             "The provided %{attribute} is too small; please provide a different %{attribute} greater than %{count}."
      greater_than_or_equal_to: "The provided %{attribute} is too small; please provide a different %{attribute} greater than or equal to %{count}."
      inclusion:                "The chosen %{attribute} is not available; please choose an available option." # Rails 4
      inclusion_of:             "The chosen %{attribute} is not available; please choose an available option." # Rails 5
      invalid:                  "The provided %{attribute} is invalid; please enter a valid %{attribute}."
      in_between:               "The provided %{attribute} is outside of the accepted range; please enter a different %{attribute} within the range of %{min} to %{max}."
      less_than:                "The provided %{attribute} is too large; please provide a different %{attribute} less than %{count}."
      less_than_or_equal_to:    "The provided %{attribute} is too large; please provide a different %{attribute} less than or equal to %{count}."
      not_a_number:             "The provided %{attribute} is not numeric; please enter a numeric %{attribute}."
      odd:                      "The provided %{attribute} is even; please enter an odd %{attribute}."
      record_invalid:           "The %{attribute} is invalid. %{errors}"
      spoofed_media_type:       "The provided %{attribute} is invalid (often due to an incorrect file extension); please provide a valid %{attribute}, including an appropriate file extension."
      too_long:                 "The provided %{attribute} contains more than the %{count} available characters; please shorten the entry."
      too_short:                "The provided %{attribute} contains fewer than the %{count} required characters; please lengthen the entry."
      taken:                    "The provided %{attribute} is already taken; please enter a different %{attribute}."
      wrong_length:             "The provided %{attribute} contains the wrong amount of characters; please adjust the entry to exactly %{count} characters."
© www.soinside.com 2019 - 2024. All rights reserved.