Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

i18n: error message localization for particular model

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'}"
like image 587
fl00r Avatar asked Aug 19 '11 17:08

fl00r


1 Answers

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")
like image 186
Harish Shetty Avatar answered Nov 15 '22 06:11

Harish Shetty