2011-01-23 67 views
1

我有一个查询类,我希望它的human_readable名称在活动记录错误消息中是“搜索”。 activerecord本地化树如何寻找这种情况?在错误信息中本地化activerecord模型名称

我想:

en: 
    activerecord: 
    models: 
     query: "Search" 
    errors: 
     models: 
     query: 
      attributes: 
      name: 
       unique_within_scope: "blah blah" 

和属性的东西通过作品:

errors.add(:name, I18n.t('activerecord.errors.models.query.attributes.name.unique_within_scope')) 

模型。但我希望错误消息的翻译unique_within_scope会导致:

“搜索保存失败: 等等等等”

代替:

“查询保存失败: 等等等等”

就像现在这样。

任何帮助表示赞赏。谢谢!

回答

1

它实际上工作,我只需要重新启动服务器。

0

,而你的方法工作,这将是清洁:

errors.add(:name, :unique_within_scope) 

,然后简单地定义一个资源字符串

activerecord.errors.models.query.attributes.name.unique_within_scope 

换句话说,如果你在一个符号传递,它假定它是一个资源键,并会在您的资源包中查找它。

另外一个好处是,您还可以定义

activerecord.errors.messages.unique_within_scope 

设置默认消息对于这种类型的(由不同型号或不同性质的使用),然后可以在必要时覆盖特定模型的误差。