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

Fix that escaped interpolations with reserved keywords raised ReservedInterpolationKey #688

Merged
merged 2 commits into from
May 6, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}')
Bilka2 marked this conversation as resolved.
Show resolved Hide resolved
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