diff --git a/lib/i18n/backend/simple.rb b/lib/i18n/backend/simple.rb index b54164d4..c09acd7d 100644 --- a/lib/i18n/backend/simple.rb +++ b/lib/i18n/backend/simple.rb @@ -151,12 +151,7 @@ def pluralize(locale, entry, count) def interpolate(locale, string, values = {}) return string unless string.is_a?(String) - if string.respond_to?(:force_encoding) - original_encoding = string.encoding - string.force_encoding(Encoding::BINARY) - end - - result = string.gsub(MATCH) do + string.gsub(MATCH) do escaped, pattern, key = $1, $2, $2.to_sym if escaped @@ -169,9 +164,6 @@ def interpolate(locale, string, values = {}) values[key].to_s end end - - result.force_encoding(original_encoding) if original_encoding - result end # Loads a single translations file by delegating to #load_rb or diff --git a/test/simple_backend_test.rb b/test/simple_backend_test.rb index 40d1c2e3..7b7b1375 100644 --- a/test/simple_backend_test.rb +++ b/test/simple_backend_test.rb @@ -253,6 +253,32 @@ def test_interpolate_given_a_value_hash_interpolates_into_unicode_string assert_equal 'Häi David!', @backend.send(:interpolate, nil, 'Häi {{name}}!', :name => 'David') end + def test_interpolate_given_an_unicode_value_hash_interpolates_to_the_string + assert_equal 'Hi ゆきひろ!', @backend.send(:interpolate, nil, 'Hi {{name}}!', :name => 'ゆきひろ') + end + + def test_interpolate_given_an_unicode_value_hash_interpolates_into_unicode_string + assert_equal 'こんにちは、ゆきひろさん!', @backend.send(:interpolate, nil, 'こんにちは、{{name}}さん!', :name => 'ゆきひろ') + end + + if Kernel.const_defined?(:Encoding) + def test_interpolate_given_a_non_unicode_multibyte_value_hash_interpolates_into_a_string_with_the_same_encoding + assert_equal euc_jp('Hi ゆきひろ!'), @backend.send(:interpolate, nil, 'Hi {{name}}!', :name => euc_jp('ゆきひろ')) + end + + def test_interpolate_given_an_unicode_value_hash_into_a_non_unicode_multibyte_string_raises_encoding_compatibility_error + assert_raises(Encoding::CompatibilityError) do + @backend.send(:interpolate, nil, euc_jp('こんにちは、{{name}}さん!'), :name => 'ゆきひろ') + end + end + + def test_interpolate_given_a_non_unicode_multibyte_value_hash_into_an_unicode_string_raises_encoding_compatibility_error + assert_raises(Encoding::CompatibilityError) do + @backend.send(:interpolate, nil, 'こんにちは、{{name}}さん!', :name => euc_jp('ゆきひろ')) + end + end + end + def test_interpolate_given_nil_as_a_string_returns_nil assert_nil @backend.send(:interpolate, nil, nil, :name => 'David') end @@ -272,6 +298,12 @@ def test_interpolate_given_an_empty_values_hash_raises_missing_interpolation_arg def test_interpolate_given_a_string_containing_a_reserved_key_raises_reserved_interpolation_key assert_raises(I18n::ReservedInterpolationKey) { @backend.send(:interpolate, nil, '{{default}}', {:default => nil}) } end + + private + + def euc_jp(string) + string.encode!(Encoding::EUC_JP) + end end class I18nSimpleBackendLocalizeDateTest < Test::Unit::TestCase @@ -533,4 +565,4 @@ def test_reload_translations_uninitializes_translations @backend.reload! assert_equal @backend.initialized?, false end -end \ No newline at end of file +end