当我提交包含错误的表单时,它将返回错误消息。如何使用i18n翻译这些错误消息?我已经将 View 中的所有其他文本进行了翻译,因此我知道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
我想翻译标题和错误。
最佳答案
标题的翻译是:
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 Internationalization (I18n) API Guide中,可能会给您带来更多的见解。
关于ruby-on-rails - 如何转换事件记录模型验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7686013/