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

with_locale raises an exception if the current locale is not an available locale #625

Open
bensheldon opened this issue Apr 7, 2022 · 0 comments

Comments

@bensheldon
Copy link

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. (bensheldon/good_job#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.

i18n/lib/i18n.rb

Lines 321 to 333 in 00fc810

def with_locale(tmp_locale = nil)
if tmp_locale == nil
yield
else
current_locale = self.locale
self.locale = tmp_locale
begin
yield
ensure
self.locale = current_locale
end
end
end

Potential solutions

  • don't allow locale to be initialized in an invalid state in the first place
  • have with_locale ignore enforce_available_locales when re-assigning the current locale
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