Dmitri
Dmitri

Reputation: 2457

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!

Upvotes: 7

Views: 2909

Answers (1)

Erez Rabih
Erez Rabih

Reputation: 15788

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

Upvotes: 10

Related Questions