如何翻译活动记录模型验证
当我提交有错误的表单时,它会返回一条错误消息。如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
标题的翻译为:
对于翻译错误消息,Rails 将使用以下翻译顺序:
因此您可以添加:
它记录在 Rails 国际化 (I18n) API 指南,这可能会给您更多见解。
The translation for the title would be:
For translating the error messages, Rails will use the following order of translations:
So you could add:
It's documented in the Rails Internationalization (I18n) API Guide, which might give you some more insight.
只需使用您可以在此处找到的荷兰语翻译文件。它包含大多数(如果不是全部)ActiveRecord 验证消息的翻译。
将文件复制到项目中的
config/locales/
中。替代方法
如果您想从更新的翻译中受益,请将以下内容添加到您的
Gemfile
中,而不是手动复制翻译文件: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:rails I18n 指南很好地涵盖了这一点。
您可以将以下内容放入
config/locales/nl.yml
中来翻译属性:对于错误消息,ActiveRecord 将在以下命名空间中查找它们:
model
, < code>attribute 和value
已插值并可在您的翻译中使用,例如:The rails I18n guide covers this pretty well.
You can put the following in
config/locales/nl.yml
to translate the attributes:For the error messages, ActiveRecord will look them up in the following namespaces:
model
,attribute
andvalue
are interpolated and available in your translations, for example: