ruby-i18n / i18n

Internationalization (i18n) library for Ruby
MIT License
986 stars 411 forks source link

[BUG] #681

Open domingo2000 opened 8 months ago

domingo2000 commented 8 months ago

What I tried to do

I overwrited the I18n::ExceptionHandler to make a custom exception handling of translations in a Rails project.

When I overwrited the method without calling super, i got a MissingTranslation with key :"i18n.plural.rule" when the translation actually exists.

The given translation is resolved when not overwritting the handler, but raises when i overwrited with a dummy raise behaviour.

module I18n
  class CustomTranslationExceptionHandler < I18n::ExceptionHandler
    def call(exception, _locale, key, _options)
      if exception.is_a?(MissingTranslation)

        if Rails.env.test?
          super
        elsif Rails.env.development?
          raise exception
        else
          super
        end
      else
        raise exception
      end
    end
  end
end

I18n.exception_handler = I18n::CustomTranslationExceptionHandler.new

What I expected to happen

I expect that is the translation is not missing and the Exception Handler is overriden it does not throws error.

What actually happened

It throws #<I18n::MissingTranslation: Translation missing: es.i18n.plural.rule> error

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

Rails 6.1.7.6


What i actually belive it is happening is that in the file /lib/i18n/backend/pluralization.rb:82 it is called

def pluralizer(locale)
   pluralizers[locale] ||= I18n.t(:'i18n.plural.rule', :locale => locale, :resolve => false)
end

So in I18n::Backend::Pluralization#pluralize an error is raised when calling #pluralizer when a given pruralizer is not defined.

This is a annoying bug because does not permitt overriding the error handler in an easy and expected way because this extra error is raised form the inner behaviour of the I18n library when the Translation it does get resolved. Maybe you could wrap the exception to avoid beign throwed, or define a custom i18n.plural.rule by default so the error is not raised.

Thanks! :heart: