Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions lib/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,30 @@ def self.reserved_keys_pattern # :nodoc:
module Base
# Gets I18n configuration object.
def config
Thread.current.thread_variable_get(:i18n_config) ||
Thread.current.thread_variable_set(:i18n_config, I18n::Config.new)
case isolation_scope
when :thread
Thread.current.thread_variable_get(:i18n_config) || self.config = I18n::Config.new
when :fiber
Fiber[:i18n_config] || self.config = I18n::Config.new
end
end

# Sets I18n configuration object.
def config=(value)
Thread.current.thread_variable_set(:i18n_config, value)
case isolation_scope
when :thread
Thread.current.thread_variable_set(:i18n_config, value)
when :fiber
Fiber[:i18n_config] = value
end
end

def isolation_scope
@@isolation_scope ||= :thread
end

def isolation_scope=(scope)
@@isolation_scope = scope
end

# Write methods which delegates to the configuration object
Expand Down
34 changes: 33 additions & 1 deletion test/i18n_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ def setup
I18n.locale = :en
end

test "isolation_scope= can set the current isolation scope to Fiber[]" do
begin
assert_nothing_raised {
I18n.isolation_scope = :fiber
I18n.locale = 'de'
}
assert_equal :de, I18n.locale
assert_equal :de, Fiber[:i18n_config].locale
I18n.locale = :en
ensure
I18n.isolation_scope = :thread
end
end

test "locale= doesn't ignore junk" do
assert_raises(NoMethodError) { I18n.locale = Class }
end
Expand Down Expand Up @@ -549,7 +563,7 @@ def call(exception, locale, key, options); key; end
end
end

test "I18n.locale is preserved in Fiber context" do
test "I18n.locale is preserved in Fiber context when using Thread locals" do
I18n.available_locales = [:en, :ja]
I18n.default_locale = :ja
I18n.locale = :en
Expand All @@ -561,4 +575,22 @@ def call(exception, locale, key, options); key; end

assert_equal :en, fiber_locale
end

test "I18n.locale is preserved in Fiber context when using Fiber locals" do
begin
I18n.isolation_scope = :fiber
I18n.available_locales = [:en, :ja]
I18n.default_locale = :ja
I18n.locale = :en

fiber_locale = nil
Fiber.new do
fiber_locale = I18n.locale
end.resume

assert_equal :en, fiber_locale
ensure
I18n.isolation_scope = :thread
end
end
end