Skip to content

Commit

Permalink
Revert "store fallbacks in Thread.current"
Browse files Browse the repository at this point in the history
This reverts commit d024044.

And there is now a test to ensure that this feature does not regress again.
  • Loading branch information
radar committed Jan 4, 2021
1 parent 0a2c3ed commit dbdbc83
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/i18n/backend/fallbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
#
# I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
module I18n
@@fallbacks = nil

class << self
# Returns the current fallbacks implementation. Defaults to +I18n::Locale::Fallbacks+.
def fallbacks
Thread.current[:i18n_fallbacks] ||= I18n::Locale::Fallbacks.new
@@fallbacks ||= I18n::Locale::Fallbacks.new
end

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

Expand Down
25 changes: 25 additions & 0 deletions test/backend/fallbacks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ def setup
assert_equal 'default', I18n.t(:missing_bar, count: 1, default: 'default')
assert_equal 'default', I18n.t(:missing_bar, count: 0, default: 'default')
end

test "multi-threaded fallbacks" do
Thread.new do

end
end
end

# See Issue #534
Expand Down Expand Up @@ -156,6 +162,25 @@ def setup
end
end

# See Issue #546
class I18nBackendFallbacksLocalizeTestWithMultipleThreads < I18n::TestCase
class Backend < I18n::Backend::Simple
include I18n::Backend::Fallbacks
end

def setup
super
I18n.backend = Backend.new
I18n.enforce_available_locales = false
I18n.fallbacks = [I18n.default_locale]
store_translations(:en, time: { formats: { fallback: 'en fallback' } })
end

test "falls back to default locale - Issue #546" do
Thread.new { assert_equal 'en fallback', I18n.l(Time.now, format: :fallback, locale: "un-supported") }.join
end
end


class I18nBackendFallbacksLocalizeTest < I18n::TestCase
class Backend < I18n::Backend::Simple
Expand Down

0 comments on commit dbdbc83

Please sign in to comment.