Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

translating model attributes rails

I got the following problem with my rails application: I want to translate some model attributes like this, so that they show up correctly in the validation error messages for example.

de:
  activerecord:
    models:
      account:
        attributes:
          email:
            one: 'Email'
            other: 'Emails'

Now in the rails console I can check

I18n.t 'activerecord.models.account.attributes.email', count: 1

which returns "Email" and everything seems to be fine.

But in if I go to the browser and post a form, I get the error

I18n::InvalidPluralizationData in AccountsController#create
translation data {:attributes=>{:email=>{:one=>"Email", :other=>"Emails"}}} can not be used with :count => 1

How can I resolve this? What translation data does rails expect to get?

Thanks

like image 292
zobel Avatar asked Mar 08 '26 14:03

zobel


1 Answers

Model names and Attribute names both have their own i18n scope.

Try

activerecord.attributes.account.email

instead of

activerecord.models.account.attributes.email

(and adjust your yaml file accordingly).

See also the handy Rails Guide on the I18n API.

HTH!

like image 173
Raffael Avatar answered Mar 10 '26 02:03

Raffael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!