I'm a gem/Rails engine author that is using with_locale to step as lightly as possible within the parent Rails application. I've found an unexpected problem: 18n.with_locale will raise an exception if the current locale is not an available locale and available locales are enforced. (https://github.com/bensheldon/good_job/issues/549). Thank you for the help!
An alternative way to describe this problem is: I18n's locale/default_locale can be initialized with invalid/unavailable values (I'm not sure if this is originating with this gem or with Rails, but it results in I18n to be in what appears to be an invalid state).
[1] pry(main)> I18n.available_locales
=> [:nl, :fr]
[2] pry(main)> I18n.default_locale
=> :en
[3] pry(main)> I18n.locale
=> :en
[4] pry(main)> I18n.with_locale(:fr) { puts "temporary locale is #{I18n.locale}" }
temporary locale is fr
I18n::InvalidLocale: :en is not a valid locale
from /Users/bensheldon/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/i18n-1.10.0/lib/i18n.rb:357:in `enforce_available_locales!'
The exceptions is raised within line 330 of with_locale, when the current locale is reassigned/reset after the block.
I'm a gem/Rails engine author that is using
with_locale
to step as lightly as possible within the parent Rails application. I've found an unexpected problem:18n.with_locale
will raise an exception if the current locale is not an available locale and available locales are enforced. (https://github.com/bensheldon/good_job/issues/549). Thank you for the help!An alternative way to describe this problem is: I18n's
locale
/default_locale
can be initialized with invalid/unavailable values (I'm not sure if this is originating with this gem or with Rails, but it results in I18n to be in what appears to be an invalid state).The exceptions is raised within line 330 of
with_locale
, when the current locale is reassigned/reset after the block.https://github.com/ruby-i18n/i18n/blob/00fc8100135878af7b5cc05aa2213844dcbe4e1b/lib/i18n.rb#L321-L333
Potential solutions
locale
to be initialized in an invalid state in the first placewith_locale
ignoreenforce_available_locales
when re-assigning the current locale