Translate Rails Model Model - Doesn't Work

Does anyone have a few tips on how to translate model associations in Rails?

For example: I have a Person model that can have many phones. However, a person must have at least one Phone. I can not translate this check. The best I could do is the following:

validates_presence_of :phones, :message => "At least one phone is required." 

And on my YAML, I replaced this line to omit %{attribute} :

 format: ! '%{message}' 

This way only my message is displayed, and I avoid the display name of the untranslatable field.

It causes me a lot of headache, because some stones just do not allow me to go through :message => "something describing the error" , so I wanted to configure all error messages through my YAML.

In addition, with some models I can translate their attributes, but with others - not. For example:

 activerecord: attributes: additional_info: account_manager: "Manager" 

It works. I can see the "Manager" in my form. However, when this field has an error, Rails will display it as "Additional info account manager can't be blank" .

I tried this:

 activerecord: errors: models: additional_info: attributes: account_manager: "Manager" 

But no luck.

I read the docs, but don’t know why this is happening.

+7
source share
2 answers

Rails 3.2 has changed this behavior. The way I posted earlier is deprecated.

Now, to translate associations, you need to add a slash (rather than nesting everything). So instead:

  activerecord: attributes: person: additional_info: account_manager: "Manager" 

Now this is correct:

  activerecord: attributes: person: additional_info/account_manager: "Manager" 

In addition, I realized that has_many associations translate differently. If you want to translate them, the following example may help:

  activerecord: attributes: emails: address: "E-mail field" 

Instead of the model name, as I said above, you need to pass the name of the association, in this case emails .

Mark this comment and pull out the request for more information:

https://github.com/rails/rails/commit/c19bd4f88ea5cf56b2bc8ac0b97f59c5c89dbff7#commitcomment-619858

https://github.com/rails/rails/pull/3859

+9
source

The following are valid paths for Rails 4.1:

 # Basic Attribute on Model activerecord: attributes: #{model_class.model_name.i18n_key}: #{attribute_name}: "Localized Value" # Attribute on Nested Model activerecord: attributes: #{model_class.model_name.i18n_key}/#{association_name}: #{attribute_name}: "Localized Value" #{association_name}: #{attribute_name}: "Fallback Localized Value" 

So, given this model (which has i18n_key of :person ):

 class Person has_many :friends end 

You have these locale definitions:

 activerecord: attributes: person: first_name: "My Name" person/friends: first_name: "My Friend Name" friends: first_name: "A Friend Name" 

If your model is a namespace, for example:

 class MyApp::Person has_many :friends end 

i18n_key becomes :my_app/person , and your key / starts to wear out:

 activerecord: attributes: my_app/person: first_name: "My Name" my_app/person/friends: first_name: "My Friend Name" friends: first_name: "A Friend Name" 
+7
source

All Articles