From 5077ef9f615f224afd26b00d626d78e35496f89f Mon Sep 17 00:00:00 2001 From: Chipairon Date: Wed, 11 Jun 2014 19:11:07 +0200 Subject: [PATCH 1/2] Don't store translations for locales not set as available --- lib/i18n.rb | 4 ++++ lib/i18n/backend/simple.rb | 5 +++++ lib/i18n/config.rb | 5 +++++ test/backend/simple_test.rb | 8 ++++++++ 4 files changed, 22 insertions(+) diff --git a/lib/i18n.rb b/lib/i18n.rb index 54008836..297a6255 100644 --- a/lib/i18n.rb +++ b/lib/i18n.rb @@ -293,6 +293,10 @@ def enforce_available_locales!(locale) end end + def available_locales_initialized? + config.available_locales_initialized? + end + private # Any exceptions thrown in translate will be sent to the @@exception_handler diff --git a/lib/i18n/backend/simple.rb b/lib/i18n/backend/simple.rb index 00a887b4..0ccde48c 100644 --- a/lib/i18n/backend/simple.rb +++ b/lib/i18n/backend/simple.rb @@ -29,6 +29,11 @@ def initialized? # translations will be overwritten by new ones only at the deepest # level of the hash. def store_translations(locale, data, options = {}) + if I18n.available_locales_initialized? && + I18n.available_locales.include?(locale.to_sym) == false && + I18n.available_locales.include?(locale.to_s) == false + return translations + end locale = locale.to_sym translations[locale] ||= {} data = data.deep_symbolize_keys diff --git a/lib/i18n/config.rb b/lib/i18n/config.rb index fa16667f..b3736896 100644 --- a/lib/i18n/config.rb +++ b/lib/i18n/config.rb @@ -57,6 +57,11 @@ def available_locales=(locales) @@available_locales = nil if @@available_locales.empty? @@available_locales_set = nil end + + # Returns true if the available_locales have been initialized + def available_locales_initialized? + ( !!defined?(@@available_locales) && !!@@available_locales ) + end # Clears the available locales set so it can be recomputed again after I18n # gets reloaded. diff --git a/test/backend/simple_test.rb b/test/backend/simple_test.rb index 4d0c447f..c24ff753 100644 --- a/test/backend/simple_test.rb +++ b/test/backend/simple_test.rb @@ -70,6 +70,14 @@ def setup assert_equal Hash[:'en', {:foo => {:bar => 'bar', :baz => 'baz'}}], translations end + test "simple store_translations: do not store translations for locales not explicitly marked as available" do + 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 Hash[:foo, {:bar => 'bares', :baz => 'bazes'}], translations[:es] + end + # reloading translations test "simple reload_translations: unloads translations" do From 2b09acb606bc98d81ac64021ced3a8741eef246f Mon Sep 17 00:00:00 2001 From: stereobooster Date: Wed, 8 Nov 2017 10:09:24 +0100 Subject: [PATCH 2/2] Fix CR notes --- lib/i18n/backend/simple.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/i18n/backend/simple.rb b/lib/i18n/backend/simple.rb index 0ccde48c..ce690484 100644 --- a/lib/i18n/backend/simple.rb +++ b/lib/i18n/backend/simple.rb @@ -30,9 +30,9 @@ def initialized? # level of the hash. def store_translations(locale, data, options = {}) if I18n.available_locales_initialized? && - I18n.available_locales.include?(locale.to_sym) == false && - I18n.available_locales.include?(locale.to_s) == false - return translations + !I18n.available_locales.include?(locale.to_sym) && + !I18n.available_locales.include?(locale.to_s) + return data end locale = locale.to_sym translations[locale] ||= {}