Skip to content

Commit

Permalink
Use Concurrent::Map for Simple backend
Browse files Browse the repository at this point in the history
For #554
  • Loading branch information
radar committed Feb 1, 2021
1 parent 76fc94c commit 7be5b46
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/i18n.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'concurrent/map'
require 'concurrent/hash'

require 'i18n/version'
require 'i18n/exceptions'
Expand Down Expand Up @@ -33,7 +34,7 @@ module I18n
EMPTY_HASH = {}.freeze

def self.new_double_nested_cache # :nodoc:
Concurrent::Map.new { |h,k| h[k] = Concurrent::Map.new }
Concurrent::Map.new { |h, k| h[k] = Concurrent::Map.new }
end

module Base
Expand Down
4 changes: 2 additions & 2 deletions lib/i18n/backend/simple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def store_translations(locale, data, options = EMPTY_HASH)
return data
end
locale = locale.to_sym
translations[locale] ||= {}
translations[locale] ||= Concurrent::Hash.new
data = data.deep_symbolize_keys
translations[locale].deep_merge!(data)
end
Expand Down Expand Up @@ -71,7 +71,7 @@ def translations(do_init: false)
# call `init_translations`
init_translations if do_init && !initialized?

@translations ||= {}
@translations ||= Concurrent::Hash.new { |h, k| h[k] = Concurrent::Hash.new }
end

protected
Expand Down
2 changes: 1 addition & 1 deletion test/backend/simple_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def setup
I18n.available_locales = [:en, :es]
store_translations(:fr, :foo => {:bar => 'barfr', :baz => 'bazfr'})
store_translations(:es, :foo => {:bar => 'bares', :baz => 'bazes'})
assert_nil translations[:fr]
assert_equal translations[:fr], {}
assert_equal Hash[:foo, {:bar => 'bares', :baz => 'bazes'}], translations[:es]
ensure
I18n.config.enforce_available_locales = false
Expand Down

0 comments on commit 7be5b46

Please sign in to comment.