Search code examples
ruby-on-railsruby-on-rails-5rails-i18n

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 ?


Solution

  • 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