From 16813c82571afe3c4cad5a698d8fbf3ac053fb5b Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 11 Oct 2021 18:39:58 +0200 Subject: [PATCH] Exclude MissingTranslation options that are not used by the instance --- lib/i18n/exceptions.rb | 4 +++- test/backend/cache_test.rb | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/i18n/exceptions.rb b/lib/i18n/exceptions.rb index 74b3d34e..212a9039 100644 --- a/lib/i18n/exceptions.rb +++ b/lib/i18n/exceptions.rb @@ -47,10 +47,12 @@ def initialize(filename, exception_message) class MissingTranslation < ArgumentError module Base + PERMITTED_KEYS = [:scope].freeze + attr_reader :locale, :key, :options def initialize(locale, key, options = EMPTY_HASH) - @key, @locale, @options = key, locale, options.dup + @key, @locale, @options = key, locale, options.slice(*PERMITTED_KEYS) options.each { |k, v| self.options[k] = v.inspect if v.is_a?(Proc) } end diff --git a/test/backend/cache_test.rb b/test/backend/cache_test.rb index bd2efbce..04eecdc0 100644 --- a/test/backend/cache_test.rb +++ b/test/backend/cache_test.rb @@ -58,6 +58,14 @@ def teardown # assert_raise(I18n::MissingTranslationData) { I18n.t(:missing, :raise => true) } end + test "MissingTranslationData does not cache custom options" do + I18n.t(:missing, :scope => :foo, :extra => true) + assert_equal 1, I18n.cache_store.instance_variable_get(:@data).size + + cache_key, entry = I18n.cache_store.instance_variable_get(:@data).first + assert_equal({ scope: :foo }, entry.value.options) + end + test "uses 'i18n' as a cache key namespace by default" do assert_equal 0, I18n.backend.send(:cache_key, :en, :foo, {}).index('i18n') end