如何翻译活动记录模型验证

发布于 2024-12-09 04:25:11 字数 365 浏览 0 评论 0原文

当我提交有错误的表单时,它会返回一条错误消息。如何使用 i18n 翻译这些错误消息?我已经翻译了我视图中的所有其他文本,所以我知道 l18n 在 Rails 中是如何工作的。我现在明白了:

2 errors prohibited this user from being saved:

Email translation missing: nl.activerecord.errors.models.user.attributes.email.blank
Email translation missing: nl.activerecord.errors.models.user.attributes.email.blank

我想翻译标题和错误。

When I submit a form with an error in it, it returns an error message. How can I translate these error messages with i18n? I already have translation for all the other texts in my views, so I know how l18n works in Rails. I now get this:

2 errors prohibited this user from being saved:

Email translation missing: nl.activerecord.errors.models.user.attributes.email.blank
Email translation missing: nl.activerecord.errors.models.user.attributes.email.blank

I want to translate both the title and the errors.

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

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

发布评论

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

评论(3

假装爱人 2024-12-16 04:25:11

标题的翻译为:

nl:
  activerecord:
    errors:
      template:
        header:
          one:   "1 error prohibited this %{model} from being saved"
          other: "%{count} errors prohibited this %{model} from being saved"
        body:    "There were problems with the following fields:"

对于翻译错误消息,Rails 将使用以下翻译顺序:

activerecord.errors.models.user.attributes.name.blank
activerecord.errors.models.user.blank
activerecord.errors.messages.blank
errors.attributes.name.blank
errors.messages.blank

因此您可以添加:

nl:
  activerecord:
    errors:
      models:
        user:
          attributes:
            email:
              blank: "foo blank in nl bar baz"

它记录在 Rails 国际化 (I18n) API 指南,这可能会给您更多见解。

The translation for the title would be:

nl:
  activerecord:
    errors:
      template:
        header:
          one:   "1 error prohibited this %{model} from being saved"
          other: "%{count} errors prohibited this %{model} from being saved"
        body:    "There were problems with the following fields:"

For translating the error messages, Rails will use the following order of translations:

activerecord.errors.models.user.attributes.name.blank
activerecord.errors.models.user.blank
activerecord.errors.messages.blank
errors.attributes.name.blank
errors.messages.blank

So you could add:

nl:
  activerecord:
    errors:
      models:
        user:
          attributes:
            email:
              blank: "foo blank in nl bar baz"

It's documented in the Rails Internationalization (I18n) API Guide, which might give you some more insight.

故事灯 2024-12-16 04:25:11

只需使用您可以在此处找到的荷兰语翻译文件。它包含大多数(如果不是全部)ActiveRecord 验证消息的翻译。

将文件复制到项目中的 config/locales/ 中。

替代方法

如果您想从更新的翻译中受益,请将以下内容添加到您的Gemfile中,而不是手动复制翻译文件:

gem 'rails-i18n'

Just use the Dutch translations file that you can find here. It contains translations for most (if not all) ActiveRecord validation messages.

Copy the file to config/locales/ in your project.

Alternate method

If you want to benefit from updated translations, add the following to your Gemfile instead of copying the translation files by hand:

gem 'rails-i18n'
抚你发端 2024-12-16 04:25:11

rails I18n 指南很好地涵盖了这一点。

您可以将以下内容放入 config/locales/nl.yml 中来翻译属性:

nl:
  activerecord:
    models:
      user: "User"
    attributes:
      email: "Email"

对于错误消息,ActiveRecord 将在以下命名空间中查找它们:

activerecord.errors.models.[model_name].attributes.[attribute_name]
activerecord.errors.models.[model_name]
activerecord.errors.messages
errors.attributes.[attribute_name]
errors.messages

model, < code>attribute 和 value 已插值并可在您的翻译中使用,例如:

nl:
  errors:
    messages:
      blank: "Please fill the %{model}'s %{attribute}"

The rails I18n guide covers this pretty well.

You can put the following in config/locales/nl.yml to translate the attributes:

nl:
  activerecord:
    models:
      user: "User"
    attributes:
      email: "Email"

For the error messages, ActiveRecord will look them up in the following namespaces:

activerecord.errors.models.[model_name].attributes.[attribute_name]
activerecord.errors.models.[model_name]
activerecord.errors.messages
errors.attributes.[attribute_name]
errors.messages

model, attribute and value are interpolated and available in your translations, for example:

nl:
  errors:
    messages:
      blank: "Please fill the %{model}'s %{attribute}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文