Forum: Rails I18n Use custom I18n messages in a model validation

Fee5ffeec5750ef42697fdfe6e1348c4?d=identicon&s=25 Peter (Guest)
on 2010-08-12 10:56
(Received via mailing list)
I would like to show custom translated error messages (based on the
locale in the URL) for my validations. I have to following:

validates :phone, :format => { :with => /^(0{2}|\+)/, :message =>
I18n.t('validation.need_international_format') }

First, is this the correct way to add custom validation messages or am
I doing it wrong?

Now, if I understand this correctly the model is only loaded once, at
boot time. So I think the message is also only evaluated once, again,
at boot time. If my default locale is English it means the English
message is used. Now, if I switch to French without restarting the
application Rails will use the French message without reevaluating the
model?! How is this done?

Thanks!
4a551074ddba4460f95d011c47190d0e?d=identicon&s=25 Henrik --- (malesca)
on 2010-08-12 13:21
(Received via mailing list)
On Thu, Aug 12, 2010 at 10:50, Peter <hamfilter@gmail.com> wrote:
> I would like to show custom translated error messages (based on the
> locale in the URL) for my validations. I have to following:
>
> validates :phone, :format => { :with => /^(0{2}|\+)/, :message =>
> I18n.t('validation.need_international_format') }
>
> First, is this the correct way to add custom validation messages or am
> I doing it wrong?

Use :message => :need_international_format and e.g.

en:
  activerecord:
    errors:
      models:
        customer:
          need_international_format: "must be in the international
format"

See http://guides.rubyonrails.org/i18n.html for more info.
Fee5ffeec5750ef42697fdfe6e1348c4?d=identicon&s=25 Peter (Guest)
on 2010-08-12 17:34
(Received via mailing list)
Great, this works much better! Thanks Henrik.
This topic is locked and can not be replied to.