Ruby-on-rails 如何翻译活动记录模型验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7686013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:06:47  来源:igfitidea点击:

How to translate active record model validations

ruby-on-railsactiverecordinternationalization

提问by John

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:

当我提交一个包含错误的表单时,它会返回一条错误消息。如何使用 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

I want to translate both the title and the errors.

我想翻译标题和错误。

回答by tbuehlmann

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:

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

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.

它记录在Rails Internationalization (I18n) API Guide 中,它可能会给您一些更多的见解。

回答by rdvdijk

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

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

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

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

Alternate method

替代方法

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

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

gem 'rails-i18n'

回答by Benoit Garret

The rails I18n guidecovers this pretty well.

轨道的I18n指南包括这相当不错。

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

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

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

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

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

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

model, attributeand valueare interpolated and available in your translations, for example:

modelattributevalue在您的翻译中插入并可用,例如:

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