Skip to content

Commit

Permalink
Exclude MissingTranslation options that are not used by the instance
Browse files Browse the repository at this point in the history
Daniel authored and movermeyer committed Mar 16, 2022

Verified

This commit was signed with the committer’s verified signature.
1 parent d763372 commit 5191ee0
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/i18n/exceptions.rb
Original file line number Diff line number Diff line change
@@ -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

8 changes: 8 additions & 0 deletions test/backend/cache_test.rb
Original file line number Diff line number Diff line change
@@ -58,6 +58,14 @@ def teardown
# assert_raises(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

0 comments on commit 5191ee0

Please sign in to comment.