From 913e40e44ca301c0449299419b3ffda2a94ad520 Mon Sep 17 00:00:00 2001 From: Bogdan Gusiev Date: Sat, 31 Aug 2013 18:26:49 +0300 Subject: [PATCH] Fix I18n.interpolate when ActiveSupport::SafeBuffer passed as argument --- lib/i18n/interpolate/ruby.rb | 2 +- test/i18n/interpolate_test.rb | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/i18n/interpolate/ruby.rb b/lib/i18n/interpolate/ruby.rb index 442677f2..d2fdda75 100644 --- a/lib/i18n/interpolate/ruby.rb +++ b/lib/i18n/interpolate/ruby.rb @@ -22,7 +22,7 @@ def interpolate_hash(string, values) if match == '%%' '%' else - key = ($1 || $2).to_sym + key = ($1 || $2 || match.tr("%{}", "")).to_sym value = if values.key?(key) values[key] else diff --git a/test/i18n/interpolate_test.rb b/test/i18n/interpolate_test.rb index 7041b322..59f26791 100644 --- a/test/i18n/interpolate_test.rb +++ b/test/i18n/interpolate_test.rb @@ -58,6 +58,17 @@ class I18nInterpolateTest < Test::Unit::TestCase def test_sprintf_mix_unformatted_and_formatted_named_placeholders assert_equal "foo 1.000000", I18n.interpolate("%{name} %f", :name => "foo", :num => 1.0) end + + class RailsSafeBuffer < String + + def gsub(*args, &block) + to_str.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 end class I18nMissingInterpolationCustomHandlerTest < Test::Unit::TestCase