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

Return same string object when no interpolations were made #649

Merged
merged 1 commit into from
Feb 28, 2023
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
8 changes: 7 additions & 1 deletion lib/i18n/interpolate/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ def interpolate(string, values)

def interpolate_hash(string, values)
pattern = INTERPOLATION_PATTERNS_CACHE[config.interpolation_patterns]
string.gsub(pattern) do |match|
interpolated = false

interpolated_string = string.gsub(pattern) do |match|
interpolated = true

if match == '%%'
'%'
else
Expand All @@ -42,6 +46,8 @@ def interpolate_hash(string, values)
$3 ? sprintf("%#{$3}", value) : value
end
end

interpolated ? interpolated_string : string
end
end
end
6 changes: 6 additions & 0 deletions test/i18n/interpolate_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,15 @@ def gsub(*args, &block)
end

end

test "with String subclass that redefined gsub method" do
assert_equal "Hello mars world", I18n.interpolate(RailsSafeBuffer.new("Hello %{planet} world"), :planet => 'mars')
end

test "with String subclass that redefined gsub method returns same object if no interpolations" do
string = RailsSafeBuffer.new("Hello world")
assert_same string, I18n.interpolate(string, :planet => 'mars')
end
end

class I18nMissingInterpolationCustomHandlerTest < I18n::TestCase
Expand Down