Ruby on Rails i18n - Want To Translate Custom Messages in Models

前端 未结 4 1771
星月不相逢
星月不相逢 2020-12-13 09:19

I have attributes with special validation where I use the message clause to display a special message just for that validation. Here is one example:

validat         


        
4条回答
  •  春和景丽
    2020-12-13 10:00

    I just walked through all this and found the rails guides for custom validators too hard-coded... I'm posting this here even though it's not exactly what you asked, but the Q title fits perfectly (which is why I read this post for my problem).

    Custom validation with i18n message:

    validate :something_custom?, if: :some_trigger_condition
    
    def something_custom?
      if some_error_condition
        errors.add(:some_field_key, :some_custom_msg)
      end
    end
    
    # en.yml
    activerecord: 
      errors:
        models:
          some_model:
            some_custom_msg: "This is i18n controlled. yay!"
    

提交回复
热议问题