Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] #681

Open
domingo2000 opened this issue Feb 15, 2024 · 0 comments
Open

[BUG] #681

domingo2000 opened this issue Feb 15, 2024 · 0 comments

Comments

@domingo2000
Copy link

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! ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant