如何本地化 rails 活动记录验证中的自定义错误消息?

How do I localize custom error messages in rails active record validations?

具体来说,我试图本地化 自定义方法 示例 here. I found a similar question,但我不知道如何传递 :message errors.add() 方法中的参数。我试过这样做:

errors.add(:discount, message: :greater_than_value_error)

但它打印:

{:message=>:greater_than_value_error} 

而不是打印 .yml 文件中的实际错误消息。

此处正确的语法应该是什么?

我的 .yml 文件看起来像这样(不确定下面这段是否 100% 准确):

activerecord: #try to use activemodel here
  errors:
    models:
      invoice: # or namespace/model_name
        attributes:
          discount:
            greater_than_value_error: "can't be greater than total value"

尝试跟随 errors.add :field_name, :message 和 rails 4.

errors.add(:discount, :greater_than_value_error)

如果您正在编写自定义验证,那么您需要进行实际翻译,

errors.add(:discount, I18n.t(path_to_locale_text))

只有在使用内置方法时,您才可以国际化标准验证消息,例如预定义验证器 greater_than_value_error

validates_numericality_of :discount :greater_than => limit

查看 了解更多详情。