Skip to content

Commit be2f3a0

Browse files
committed
Return same string object when no interpolations were made
1 parent f75520a commit be2f3a0

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/i18n/interpolate/ruby.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ def interpolate(string, values)
2828

2929
def interpolate_hash(string, values)
3030
pattern = INTERPOLATION_PATTERNS_CACHE[config.interpolation_patterns]
31-
string.gsub(pattern) do |match|
31+
interpolated = false
32+
33+
interpolated_string = string.gsub(pattern) do |match|
34+
interpolated = true
35+
3236
if match == '%%'
3337
'%'
3438
else
@@ -42,6 +46,8 @@ def interpolate_hash(string, values)
4246
$3 ? sprintf("%#{$3}", value) : value
4347
end
4448
end
49+
50+
interpolated ? interpolated_string : string
4551
end
4652
end
4753
end

test/i18n/interpolate_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,15 @@ def gsub(*args, &block)
6565
end
6666

6767
end
68+
6869
test "with String subclass that redefined gsub method" do
6970
assert_equal "Hello mars world", I18n.interpolate(RailsSafeBuffer.new("Hello %{planet} world"), :planet => 'mars')
7071
end
72+
73+
test "with String subclass that redefined gsub method returns same object if no interpolations" do
74+
string = RailsSafeBuffer.new("Hello world")
75+
assert_same string, I18n.interpolate(string, :planet => 'mars')
76+
end
7177
end
7278

7379
class I18nMissingInterpolationCustomHandlerTest < I18n::TestCase

0 commit comments

Comments
 (0)