tansengming / stripe-rails

A Rails Engine for integrating with Stripe
MIT License
753 stars 123 forks source link

2.2.0 fails with `enforce_available_locales!': :en is not a valid locale (I18n::InvalidLocale) #206

Open renchap opened 3 years ago

renchap commented 3 years ago

After upgrading to stripe-rails 2.2.0 (or 2.2.1), I am getting this error when running rails c or rails s

…
         8: from /Users/renchap/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/stripe-rails-2.2.0/lib/stripe/prices.rb:1:in `<main>'
         7: from /Users/renchap/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/stripe-rails-2.2.0/lib/stripe/prices.rb:2:in `<module:Stripe>'
         6: from /Users/renchap/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/stripe-rails-2.2.0/lib/stripe/prices.rb:6:in `<module:Prices>'
         5: from /Users/renchap/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/stripe-rails-2.2.0/lib/stripe/configuration_builder.rb:14:in `configuration_for'
         4: from /Users/renchap/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/stripe-rails-2.2.0/lib/stripe/configuration_builder.rb:14:in `class_eval'
         3: from /Users/renchap/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/stripe-rails-2.2.0/lib/stripe/prices.rb:34:in `block in <module:Prices>'
         2: from /Users/renchap/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/activesupport-6.0.3.4/lib/active_support/core_ext/array/conversions.rb:70:in `to_sentence'
         1: from /Users/renchap/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/i18n-1.8.6/lib/i18n.rb:198:in `translate'
/Users/renchap/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/i18n-1.8.6/lib/i18n.rb:342:in `enforce_available_locales!': :en is not a valid locale (I18n::InvalidLocale)

I am not sure what is happening here as my locales are defined as config.i18n.available_locales = %i[en fr]

tansengming commented 3 years ago

I tried to recreate this problem on https://github.com/tansengming/stripe-rails-dummy/tree/issue-206-test

it has

I also added config.i18n.available_locales = %i[en fr] to application.rb just to be sure. But I can't seem to recreate the error. Can you think of anything else I'm missing?

renchap commented 3 years ago

Sorry for the long delay.

I managed to reproduce it: it depends on the location of the stripe-rails gem in your Gemfile!

For me it was near the end and the error occurred, but if I move it at the top of my Gemfile is works fine.

I added some debugging to I18n and with stripe-rails loaded first, I can see that it is called once with the default config (empty) and then with my own config:

In i18n.rb line 336:

      pp locale
      pp I18n.config.available_locales_set
      pp I18n.config.available_locales
$ rails c
:en
#<Set: {"en", :en}>
[:en]
:en
#<Set: {"en", :en}>
["en", "fr"]

With the gem loaded later, available_locales is empty:

:en
#<Set: {}>
[]

I guess stripe-i18n should only load locale-dependent code once the app has been initialized by Rails, not when the gem is loaded, so it is no-longer order dependent and will use the correct I18n config.