Skip to content

Commit

Permalink
Merge pull request #581 from sundling/missingtranslation-options
Browse files Browse the repository at this point in the history
Exclude MissingTranslation options that are not used by the instance
  • Loading branch information
radar committed Jan 26, 2022
2 parents d763372 + 16813c8 commit 92cef5e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/i18n/exceptions.rb
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions test/backend/cache_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit 92cef5e

Please sign in to comment.