Skip to content

Commit

Permalink
Merge pull request #688 from Bilka2/escaped-reserved-interpolation
Browse files Browse the repository at this point in the history
Fix that escaped interpolations with reserved keywords raised ReservedInterpolationKey
  • Loading branch information
radar committed May 6, 2024
2 parents 5bec0bc + 8f4bca1 commit 2ab6eb0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def self.reserve_key(key)
end

def self.reserved_keys_pattern # :nodoc:
@reserved_keys_pattern ||= /%\{(#{RESERVED_KEYS.join("|")})\}/
@reserved_keys_pattern ||= /(?<!%)%\{(#{RESERVED_KEYS.join("|")})\}/
end

module Base
Expand Down
18 changes: 18 additions & 0 deletions lib/i18n/tests/interpolation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,24 @@ module Interpolation
assert_raises(I18n::ReservedInterpolationKey) { interpolate(:interpolate) }
end

test "interpolation: it does not raise I18n::ReservedInterpolationKey for escaped variables" do
assert_nothing_raised do
assert_equal '%{separator}', interpolate(:foo => :bar, :default => '%%{separator}')
end

# Note: The two interpolations below do not remove the escape character (%) because
# I18n should not alter the strings when no interpolation parameters are given,
# see the comment at the top of this file.
assert_nothing_raised do
assert_equal '%%{scope}', interpolate(:default => '%%{scope}')
end

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

test "interpolation: deep interpolation for default string" do
assert_equal 'Hi %{name}!', interpolate(:default => 'Hi %{name}!', :deep_interpolation => true)
end
Expand Down

0 comments on commit 2ab6eb0

Please sign in to comment.