i18n:特定型号的错误消息本地化

时间:2011-08-19 17:21:14

标签: ruby-on-rails ruby localization internationalization

我可以为每个验证本地化错误消息,但是如何为特定模型创建错误。

普通语言环境如下所示:

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

但我想更改user型号的消息:

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

1 个答案:

答案 0 :(得分:30)

试试这个:

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

<强>参考:

ActiveRecord code comments

<强> PS: 本地化字符串应使用插值变量进行动态替换。

E.g: 在login对象的user字段上添加错误:

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