Rails translates validation error messages for nested i18n attributes

The error message does not translate my nested model attributes, because it is defined as the only one, but in error messages it looks like the plural.

I have a "Man" model that has mana: addresses. This Person model accepts nested "Addresses" attributes. I create Addresses only with the Person model.

my locale file looks like

en: activerecord: models: person: one: "Person" other: "People" address: one: 'Address' other: 'Addresses' attributes: person: first_name: 'First name' last_name: 'Last name' middle_name: 'Middel name' address: street: street city: city country: country 

and for the error message:

 en: errors: &errors format: ! '%{attribute} %{message}' messages: blank: can't be blank 

It works with single models, but with nested attributes I have a problem with validation messages.

Because the message is displayed as follows:

  @messages= {:first_name=>["can't be blank"], :last_name=>["can't be blank"], :"addresses.street"=>["can't be blank"], :"addresses.city"=>["can't be blank"]}> 

the search does not find a translation for address.street, since it is just address.street in the yml file.

How can I force find find.street when it searches for address.street without double all of my entries?

+6
source share
1 answer

You searched for how to handle pluralization in the Rails Doc

[rails i18n] [1] [1]: http://guides.rubyonrails.org/i18n.html#pluralization

3 Pluralization In English, there is only one single and one plural form for a given string, for example. "1 message" and "2 messages." Other languages ​​(Arabic, Japanese, Russian and many others) have different grammars, which have additional or smaller plural forms. In this way, the I18n API provides a flexible pluralization function.

-1
source

Source: https://habr.com/ru/post/952614/


All Articles