Skip to content

Commit

Permalink
Merge pull request #678 from fatkodima/fix-interpolate-reserved-keywords
Browse files Browse the repository at this point in the history
Raise when translated entry contains interpolations for reserved keywords and no substitutions provided
  • Loading branch information
radar authored Nov 14, 2023
2 parents 59674c2 + d1762c6 commit baf8a88
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
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

0 comments on commit baf8a88

Please sign in to comment.