Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude MissingTranslation options that are not used by the instance #581

Merged
merged 1 commit into from Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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_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
Expand Down