LapinLove404
LapinLove404

Reputation: 1949

How to pluralize human_attribute_name with Rails 5 and I18n?

I want to translate and pluralize an attribute name in my App.

So, for the my api_key attribute, I have the translation "Clef API".

I pluralize the attribute name with Mymodel.human_attribute_name(:api_key).pluralize(2) and get as a result : "Clef APIs"

But in french, the plural for "Clef API" is "Clefs API"

So I tried to give plural translations in my YML but it does not work and I stillget the transaltion for "One" pluralized with an 's' at the end.

fr.yml :

activerecord:
  attributes:
    mymodel:
      api_key:
        one: "Clef API"
        other: "Clefs API"  

Can I get Mymodel.human_attribute_name(:api_key).pluralize(2) to return "Clefs API" ?

If not, how could I translate and pluralize my atributes name ?

Upvotes: 3

Views: 1730

Answers (2)

uday
uday

Reputation: 1481

You can add customized plural words in Inflectors at config/initializers/inflections.rb. You can see the code example below:

ActiveSupport::Inflector.inflections(:fr) do |inflect|
  inflect.irregular 'Clef API', 'Clefs API'
end

Upvotes: 3

LapinLove404
LapinLove404

Reputation: 1949

Just after posting my question I though I should check the doc for human_attribute_name for a hint. And it did solve my problem...

Turns out human_attribute_name handles pluralization directly. It accepts an options hash with the key :count that does trigger the plural like with other I18n methods.

So, it's as simple as doing Mymodel.human_attribute_name(:api_key, count: 2)

Upvotes: 7

Related Questions