i18n: error message localization for particular model

后端 未结 1 1192
小蘑菇
小蘑菇 2021-02-04 13:56

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:
         


        
相关标签:
1条回答
  • 2021-02-04 14:32

    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")
    
    0 讨论(0)
提交回复
热议问题