From e264dca1e91ec83af5263cce7152d4101660daf4 Mon Sep 17 00:00:00 2001 From: jonian Date: Mon, 4 Jan 2021 15:32:12 +0200 Subject: [PATCH 1/4] update multi-threaded fallbacks test --- test/backend/fallbacks_test.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/test/backend/fallbacks_test.rb b/test/backend/fallbacks_test.rb index 99565580..a3b0dcd0 100644 --- a/test/backend/fallbacks_test.rb +++ b/test/backend/fallbacks_test.rb @@ -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 From 1f2e32829321fd9faf9be285ba51d43d9a06c5d6 Mon Sep 17 00:00:00 2001 From: jonian Date: Mon, 4 Jan 2021 16:10:36 +0200 Subject: [PATCH 2/4] store fallbacks in Thread.current and fetch with @@fallbacks as default --- lib/i18n/backend/fallbacks.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/i18n/backend/fallbacks.rb b/lib/i18n/backend/fallbacks.rb index ce8635bb..b6437620 100644 --- a/lib/i18n/backend/fallbacks.rb +++ b/lib/i18n/backend/fallbacks.rb @@ -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.fetch(: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 From 9d4ee1abf34b5a7de4092d1643ddcf2f5ef7f759 Mon Sep 17 00:00:00 2001 From: jonian Date: Mon, 4 Jan 2021 16:37:51 +0200 Subject: [PATCH 3/4] make thread key fetching compatible with older rubies --- lib/i18n/backend/fallbacks.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/i18n/backend/fallbacks.rb b/lib/i18n/backend/fallbacks.rb index b6437620..ed2f7196 100644 --- a/lib/i18n/backend/fallbacks.rb +++ b/lib/i18n/backend/fallbacks.rb @@ -16,7 +16,7 @@ class << self # Returns the current fallbacks implementation. Defaults to +I18n::Locale::Fallbacks+. def fallbacks @@fallbacks ||= I18n::Locale::Fallbacks.new - Thread.current.fetch(:i18n_fallbacks, @@fallbacks) + Thread.current.key?(:i18n_fallbacks) ? Thread.current[:i18n_fallbacks] : @@fallbacks end # Sets the current fallbacks implementation. Use this to set a different fallbacks implementation. From 45bbb7dd1c88285858794a862d7dc053a0d5abb3 Mon Sep 17 00:00:00 2001 From: jonian Date: Mon, 4 Jan 2021 16:52:39 +0200 Subject: [PATCH 4/4] fix thread key fetching --- lib/i18n/backend/fallbacks.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/i18n/backend/fallbacks.rb b/lib/i18n/backend/fallbacks.rb index ed2f7196..7688c13d 100644 --- a/lib/i18n/backend/fallbacks.rb +++ b/lib/i18n/backend/fallbacks.rb @@ -16,7 +16,7 @@ class << self # Returns the current fallbacks implementation. Defaults to +I18n::Locale::Fallbacks+. def fallbacks @@fallbacks ||= I18n::Locale::Fallbacks.new - Thread.current.key?(:i18n_fallbacks) ? Thread.current[:i18n_fallbacks] : @@fallbacks + Thread.current[:i18n_fallbacks] || @@fallbacks end # Sets the current fallbacks implementation. Use this to set a different fallbacks implementation.