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

Raise when translated entry contains interpolations for reserved keywords and no substitutions provided #678

Merged
merged 1 commit into from
Nov 14, 2023
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/backend/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ def translate(locale, key, options = EMPTY_HASH)

deep_interpolation = options[:deep_interpolation]
values = Utils.except(options, *RESERVED_KEYS) unless options.empty?
if values
if values && !values.empty?
entry = if deep_interpolation
deep_interpolate(locale, entry, values)
else
interpolate(locale, entry, values)
end
elsif entry.is_a?(String) && entry =~ I18n.reserved_keys_pattern
raise ReservedInterpolationKey.new($1.to_sym, entry)
end
entry
end
Expand Down
4 changes: 4 additions & 0 deletions lib/i18n/tests/interpolation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ module Interpolation
assert_raises(I18n::ReservedInterpolationKey) { interpolate(:foo => :bar, :default => '%{default}') }
assert_raises(I18n::ReservedInterpolationKey) { interpolate(:foo => :bar, :default => '%{separator}') }
assert_raises(I18n::ReservedInterpolationKey) { interpolate(:foo => :bar, :default => '%{scope}') }
assert_raises(I18n::ReservedInterpolationKey) { interpolate(:default => '%{scope}') }

I18n.backend.store_translations(:en, :interpolate => 'Hi %{scope}!')
assert_raises(I18n::ReservedInterpolationKey) { interpolate(:interpolate) }
end

test "interpolation: deep interpolation for default string" do
Expand Down