Skip to content

Commit 2ab6eb0

Browse files
authored
Merge pull request #688 from Bilka2/escaped-reserved-interpolation
Fix that escaped interpolations with reserved keywords raised ReservedInterpolationKey
2 parents 5bec0bc + 8f4bca1 commit 2ab6eb0

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/i18n.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def self.reserve_key(key)
4848
end
4949

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

5454
module Base

lib/i18n/tests/interpolation.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,24 @@ module Interpolation
118118
assert_raises(I18n::ReservedInterpolationKey) { interpolate(:interpolate) }
119119
end
120120

121+
test "interpolation: it does not raise I18n::ReservedInterpolationKey for escaped variables" do
122+
assert_nothing_raised do
123+
assert_equal '%{separator}', interpolate(:foo => :bar, :default => '%%{separator}')
124+
end
125+
126+
# Note: The two interpolations below do not remove the escape character (%) because
127+
# I18n should not alter the strings when no interpolation parameters are given,
128+
# see the comment at the top of this file.
129+
assert_nothing_raised do
130+
assert_equal '%%{scope}', interpolate(:default => '%%{scope}')
131+
end
132+
133+
I18n.backend.store_translations(:en, :interpolate => 'Hi %%{scope}!')
134+
assert_nothing_raised do
135+
assert_equal 'Hi %%{scope}!', interpolate(:interpolate)
136+
end
137+
end
138+
121139
test "interpolation: deep interpolation for default string" do
122140
assert_equal 'Hi %{name}!', interpolate(:default => 'Hi %{name}!', :deep_interpolation => true)
123141
end

0 commit comments

Comments
 (0)