Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails I18n interpolation

everyone!

I have a small validation for my :username field, which should be from 4 to 30 characters. I wrote a validation: :length => { :within => 4..30, :message => I18n.t('activerecord.errors.range') - I wanted to display a single error message for all kinds of errors (Not like, too_long or too_short), but here's the question - can I pass both min and max values to the translation, to have something like: Username should be between 4 and 30 characters. Currently I have: range: "should be between %{count} and %{count} characters", which obviously doesn't work (made it just for checking).

Is it possible to grab these values from the range ?

Thanks everyone in advice!

like image 640
Dmitri Avatar asked Sep 03 '12 12:09

Dmitri


1 Answers

You can pass custom variable to I18n.translate method (or its shorthand - I18n.t):

I18n.t('activerecord.errors.range', :min => 4, :max => 30)

Then you can use them inside your string template with %{min} and %{max}.

It does not take it from the range automatically but this is as closest as I can think of.

Source:

Passing Variables to Translations @ rubyonrails.org

like image 109
Erez Rabih Avatar answered Oct 04 '22 00:10

Erez Rabih