From 7be5b46485440957ea4e7b6b59e280626cb86bc1 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Mon, 1 Feb 2021 16:03:14 +1100 Subject: [PATCH] Use Concurrent::Map for Simple backend For #554 --- lib/i18n.rb | 3 ++- lib/i18n/backend/simple.rb | 4 ++-- test/backend/simple_test.rb | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/i18n.rb b/lib/i18n.rb index e998b4a8..237bfb5e 100644 --- a/lib/i18n.rb +++ b/lib/i18n.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'concurrent/map' +require 'concurrent/hash' require 'i18n/version' require 'i18n/exceptions' @@ -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 diff --git a/lib/i18n/backend/simple.rb b/lib/i18n/backend/simple.rb index ed74146c..5c7268ef 100644 --- a/lib/i18n/backend/simple.rb +++ b/lib/i18n/backend/simple.rb @@ -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 @@ -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 diff --git a/test/backend/simple_test.rb b/test/backend/simple_test.rb index 46b024cc..fcecf385 100644 --- a/test/backend/simple_test.rb +++ b/test/backend/simple_test.rb @@ -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