Skip to content

Commit

Permalink
Merge pull request #649 from fatkodima/fix-interpolate-same-object
Browse files Browse the repository at this point in the history
Return same string object when no interpolations were made
  • Loading branch information
radar authored Feb 28, 2023
2 parents f75520a + be2f3a0 commit b4cd00d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
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

0 comments on commit b4cd00d

Please sign in to comment.