Skip to content

Commit

Permalink
Merge pull request #548 from jonian/thread_safe_fallbacks
Browse files Browse the repository at this point in the history
[WIP] Thread safe fallbacks
  • Loading branch information
radar authored Jan 6, 2021
2 parents ae0c6df + 45bbb7d commit 63a79cb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/i18n/backend/fallbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ class << self
# Returns the current fallbacks implementation. Defaults to +I18n::Locale::Fallbacks+.
def fallbacks
@@fallbacks ||= I18n::Locale::Fallbacks.new
Thread.current[:i18n_fallbacks] || @@fallbacks
end

# Sets the current fallbacks implementation. Use this to set a different fallbacks implementation.
def fallbacks=(fallbacks)
@@fallbacks = fallbacks.is_a?(Array) ? I18n::Locale::Fallbacks.new(fallbacks) : fallbacks
Thread.current[:i18n_fallbacks] = @@fallbacks
end
end

Expand Down
12 changes: 11 additions & 1 deletion test/backend/fallbacks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,18 @@ def setup
end

test "multi-threaded fallbacks" do
Thread.new do
I18n.fallbacks = [:en]

thread = Thread.new do
I18n.fallbacks = [:de]
end

begin
thread.join
assert_equal 'Bar in :en', I18n.t(:bar, :locale => :'pt-BR')
ensure
thread.exit
I18n.fallbacks = I18n::Locale::Fallbacks.new
end
end
end
Expand Down

0 comments on commit 63a79cb

Please sign in to comment.