ruby-i18n / i18n

Internationalization (i18n) library for Ruby
MIT License
976 stars 408 forks source link

[BUG]Rails 7 - inflections ignored when calling pluralize on string #646

Closed jdescelliers closed 1 year ago

jdescelliers commented 1 year ago

What I tried to do

config/initializers/locale.rb
I18n.load_path += Dir[Rails.root.join("config", "locales", "*.rb")]

fr = :fr

# Permitted locales available for the application
I18n.available_locales = [fr]

I18n.locale = fr

# Set default locale to something other than :en
I18n.default_locale = fr

And the inflection configuration:

# config/initializers/inflections.rb
ActiveSupport::Inflector.inflections(:fr) do |inflect|
  inflect.plural(/(s|x|z)$/i, "\\1")
end

What I expected to happen

"quizz".pluralize(locale: :fr)

Output quizz

What actually happened

"quizz".pluralize(locale: :fr)

Output quizzs

Versions of i18n, rails, and anything else you think is necessary

i18n version : 1.12.0 Rails version 7.0.4


Bonus points for providing an application or a small code example which reproduces the issue.

Use the files provided.

Oddly when I add these rules to the en locale, these rules are applied correctly. This works fine :

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.plural(/(s|x|z)$/i, "\\1")
end

"quizz".pluralize
# Output: quizz
# Which is incorrect for English but that's not the point.

In the rails console, I18n.locale returns fr

What am I missing?

radar commented 1 year ago

I suspect, 6 months later, you have found your issue out already.

For posterity's sake: pluralize takes two positional arguments:

  def pluralize(count = nil, locale = :en)

To get what you're after:

"quizz".pluralize(2, "fr")