Skip to content

Commit 2134338

Browse files
authored
Merge pull request #724 from lee266/fix/i18n-locale-thread-variable
Fix: I18n.locale reset in Fiber context by using Thread#thread_variable
2 parents d64a88d + f1aab85 commit 2134338

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

lib/i18n.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,13 @@ def self.reserved_keys_pattern # :nodoc:
5555
module Base
5656
# Gets I18n configuration object.
5757
def config
58-
Thread.current[:i18n_config] ||= I18n::Config.new
58+
Thread.current.thread_variable_get(:i18n_config) ||
59+
Thread.current.thread_variable_set(:i18n_config, I18n::Config.new)
5960
end
6061

6162
# Sets I18n configuration object.
6263
def config=(value)
63-
Thread.current[:i18n_config] = value
64+
Thread.current.thread_variable_set(:i18n_config, value)
6465
end
6566

6667
# Write methods which delegates to the configuration object

lib/i18n/middleware.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def initialize(app)
1010
def call(env)
1111
@app.call(env)
1212
ensure
13-
Thread.current[:i18n_config] = I18n::Config.new
13+
Thread.current.thread_variable_set(:i18n_config, I18n::Config.new)
1414
end
1515

1616
end

test/i18n/middleware_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ def setup
99
end
1010

1111
test "middleware initializes new config object after request" do
12-
old_i18n_config_object_id = Thread.current[:i18n_config].object_id
12+
old_i18n_config_object_id = Thread.current.thread_variable_get(:i18n_config).object_id
1313
@middleware.call({})
1414

15-
updated_i18n_config_object_id = Thread.current[:i18n_config].object_id
15+
updated_i18n_config_object_id = Thread.current.thread_variable_get(:i18n_config).object_id
1616
refute_equal updated_i18n_config_object_id, old_i18n_config_object_id
1717
end
1818

test/i18n_test.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def setup
5959
test "sets the current locale to Thread.current" do
6060
assert_nothing_raised { I18n.locale = 'de' }
6161
assert_equal :de, I18n.locale
62-
assert_equal :de, Thread.current[:i18n_config].locale
62+
assert_equal :de, Thread.current.thread_variable_get(:i18n_config).locale
6363
I18n.locale = :en
6464
end
6565

@@ -80,7 +80,7 @@ def setup
8080
begin
8181
I18n.config = self
8282
assert_equal self, I18n.config
83-
assert_equal self, Thread.current[:i18n_config]
83+
assert_equal self, Thread.current.thread_variable_get(:i18n_config)
8484
ensure
8585
I18n.config = ::I18n::Config.new
8686
end
@@ -548,4 +548,17 @@ def call(exception, locale, key, options); key; end
548548
I18n.instance_variable_set(:@reserved_keys_pattern, nil)
549549
end
550550
end
551+
552+
test "I18n.locale is preserved in Fiber context" do
553+
I18n.available_locales = [:en, :ja]
554+
I18n.default_locale = :ja
555+
I18n.locale = :en
556+
557+
fiber_locale = nil
558+
Fiber.new do
559+
fiber_locale = I18n.locale
560+
end.resume
561+
562+
assert_equal :en, fiber_locale
563+
end
551564
end

0 commit comments

Comments
 (0)