40

I have the following validation in a model:

validates_inclusion_of :whatever, :in => [true, false], :message => I18n.t('please_select_whatever')

It seems that the translation does not work in production mode: in all languages it's always the english translation that gets diplayed (probably because I set english as the default locale in my app...?).

So I am assuming that we can't translate validations in models, because models get loaded only once - when the server is booted (and then, the default locale would be applied).

Am I right? If yes, how would you solve this problem?

Thanks for your help!

3 Answers 3

54

The solution is to NOT include any custom message keys in the models, like...

:message => I18n.t('activerecord.errors.models.my_model.attributes.whatever.please_select_whatever')

The model will then apply the default message keys, for example ":inclusion" in the case of "validates_inclusion_of"

...and in config/locales/en.yml you need to have:

en:
  activerecord:
    errors:
      models:
        my_model:
          attributes:
            whatever:
              inclusion: "Please select whatever." # see default key: "inclusion"

for reference, check out the respective Rails guide:

http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models

4
  • I always forget the defaults for the activerecord error messages (and I need them every few months or so), thankfully Google helps me find this answer over and over again! Jun 14, 2012 at 15:12
  • => The better way to approach this problem is the answer by @iain below. Nov 2, 2014 at 21:18
  • 2
    This doesn't address why OPs solution didn't work: the translation was interpreted when the project initialized, which happens every request in dev, but only once at boot in prod. To support multiple languages they must be interpreted at runtime, which @iain's solution addresses. TomDogg's solution only works if you can use the same default translation in every case, which isn't always the best UX.
    – Woahdae
    Oct 30, 2015 at 22:28
  • For error messages, this is a better link
    – niborg
    Jan 15, 2020 at 19:36
18

You can use symbols, to specify translations:

validates_inclusion_of :whatever, :in => [true, false], :message => :select_whatever

And it will be translated with a particular scope. See the I18n guide for more details.

7
  • The part following ":message =>" should be something along the lines of: I18n.t('activerecord.errors.models.[model_name].attributes.[attribute_name]')
    – TomDogg
    Dec 15, 2010 at 17:28
  • Nope, this doesn't work. I suspect that in Rails, it is not possible to have translations in models (since they only get loaded once).
    – TomDogg
    Dec 15, 2010 at 17:48
  • 4
    Use a Symbol as I did and place the translation in the appropriate place (as indicated in the guide). Don't call I18n.t here. My example is right.
    – iain
    Dec 15, 2010 at 18:03
  • I forgot to mention: I'm on Rails 2.3. I suppose your solution works for Rails 3, but not for Rails 2.3?
    – TomDogg
    Dec 16, 2010 at 8:52
  • 1
    what if I need to send a variable to the i18n string. e.g. "the value %{value} is invalid" ?
    – MegaTux
    Aug 12, 2015 at 16:12
5

OK, iain answer works, but I took very long time to figure out where should I put the :select_whatever.

validates_inclusion_of :whatever, :in => [true, false], :message => :select_whatever

OK your en.yml should look like this:

en:
  errors:
    messages:
      select_whatever: "error!!"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.