# Use custom I18n messages in a model validation

**URL:** <https://www.ruby-forum.com/t/use-custom-i18n-messages-in-a-model-validation/193166>\
**Category:** Rails I18n\
**Created:** [August 12, 2010, 10:56am UTC](https://www.ruby-forum.com/t/use-custom-i18n-messages-in-a-model-validation/193166 "2010-08-12T10:56:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [August 12, 2010, 10:56am UTC](https://www.ruby-forum.com/t/use-custom-i18n-messages-in-a-model-validation/193166/1 "2010-08-12T10:56:41Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [August 12, 2010, 1:21pm UTC](https://www.ruby-forum.com/t/use-custom-i18n-messages-in-a-model-validation/193166/2 "2010-08-12T13:21:53Z")

</div>

On Thu, Aug 12, 2010 at 10:50, Peter [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 [Internationalization (I18n) — Ruby on Rails Guides](http://guides.rubyonrails.org/i18n.html) for more info.

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [August 12, 2010, 5:34pm UTC](https://www.ruby-forum.com/t/use-custom-i18n-messages-in-a-model-validation/193166/3 "2010-08-12T17:34:48Z")

</div>

Great, this works much better! Thanks Henrik.
