简体   繁体   中英

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!

You can pass custom variable to I18n translate method (or its shorthand - 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM