Skip to content

Commit

Permalink
Raise when translated entry contains interpolations for reserved keyw…
Browse files Browse the repository at this point in the history
…ords and no substitutions provided
  • Loading branch information
fatkodima committed Nov 14, 2023
1 parent 59674c2 commit d1762c6
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 d1762c6

Please sign in to comment.