ruby-on-railsrubyrails-i18nruby-on-rails-6.1

How use rails inflections with diferent languages


I using Rails 6.1.4, and I need to configure inflections in 2 languages, but when I try to use pluralize, the word not apply pluralize.

I need apply different rules in different languages, but don't working.

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.plural(/^(ox)$/i, '\1en')
  inflect.singular(/^(ox)en/i, '\1')
  inflect.irregular "person", "people"
  inflect.uncountable %w[fish sheep]
end

ActiveSupport::Inflector.inflections(:"pt-BR") do |inflect|
  inflect.plural(/$/, "s")
  inflect.plural(/^([a-zA-z]*)a$/i, '\1as')
  inflect.plural(/(s)$/i, '\1')
  inflect.plural(/^(paí)s$/i, '\1ses')
  inflect.plural(/(z|r)$/i, '\1es')
  inflect.plural(/al$/i,  "ais")
  inflect.plural(/el$/i,  "eis")
  inflect.plural(/ol$/i,  "ois")
  inflect.plural(/ul$/i,  "uis")
  inflect.plural(/([^aeiou])l$/i, '\1is')
  ...

  # Irregulares
  inflect.irregular "país", "países"
end

This is output when try apply pluralize

enter image description here


Solution

  • Please see the documentation for String#pluralize

    pluralize(count = nil, locale = :en)

    If the optional parameter locale is specified, the word will be pluralized as a word of that language. By default, this parameter is set to :en.

    You have to specify your locale if you want to use non-English pluralization rules.

    "canal".pluralize(:"pt-BR")