i18n: error message localization for particular model

不打扰是莪最后的温柔 提交于 2020-08-21 05:29:14

问题


I can localize an error message for each validation, but how can I create an error for particular model.

Ordinary locale looks like this:

en:
  mongoid:
    errors:
      messages:
        taken: "It is already taken"

But I want to change message for user model:

en:
  mongoid:
    errors:
      messages:
        taken: "It is already taken"
        user:
          taken: "It is already taken. %{link_to 'Remember password', reset_password_path'}"

回答1:


Try this:

en:
  mongoid:
    errors:
      messages:
        taken: "It is already taken"
      models:
        user:
          attributes:
            login:
              taken: "It is already taken. %{link}"

Reference:

ActiveRecord code comments

PS: The localization string should use interpolation variable for dynamic substitution.

E.g: Add an error on the login field of user object:

user.errors.add(:login, :taken, :link => "foo")


来源:https://stackoverflow.com/questions/7125127/i18n-error-message-localization-for-particular-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!