Rails i18n ActiveModel:翻译缺席验证的错误消息

发布于 2025-01-17 09:28:02 字数 575 浏览 1 评论 0原文

我在服务类中有此验证:

class Users::Updater
  include ActiveModel::Validations

  validates(:frozen_identification_data, absence: { message: 'Identification fields cannot be changed at this time' } )
end

我正在尝试将此错误消息移至Locales文件中:

en:
  activemodel:
    errors:
      users/updater:
        attributes:
          frozen_identification_data:
            absence: "Identification fields cannot be changed at this time"

但是,当我重现测试用例时,错误消息为冷冻标识数据必须为空白。我假设缺席在这里不正确,但是我在Google上找不到任何示例。有人知道如何翻译此验证吗?

I have this validation in a service class:

class Users::Updater
  include ActiveModel::Validations

  validates(:frozen_identification_data, absence: { message: 'Identification fields cannot be changed at this time' } )
end

I'm trying to move this error message into a locales file:

en:
  activemodel:
    errors:
      users/updater:
        attributes:
          frozen_identification_data:
            absence: "Identification fields cannot be changed at this time"

But when I reproduce the test case, the error message is Frozen identification data must be blank. I'm assuming absence is incorrect here, but I can't find any examples of this usage on Google. Does anyone know how to translate this validation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心不设防 2025-01-24 09:28:02

来自 Rails 指南: https://guides.rubyonrails.org/i18n.html# error-message-interpolation

对于 absence,键应该是 present

en:
  activemodel:
    errors:
      users/updater:
        attributes:
          frozen_identification_data:
            present: "Identification fields cannot be changed at this time"

From rails guides: https://guides.rubyonrails.org/i18n.html#error-message-interpolation

For absence the key should be present:

en:
  activemodel:
    errors:
      users/updater:
        attributes:
          frozen_identification_data:
            present: "Identification fields cannot be changed at this time"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文