From 4e3cd4b4da6b3499d528a99ca540eb31c8bab382 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Fri, 27 Feb 2009 15:58:05 +0100 Subject: [PATCH] Remove unnecessary string encoding handling in the i18n simple backend which made the backend break on Ruby 1.9. --- .../vendor/i18n-0.1.1/lib/i18n/backend/simple.rb | 10 +----- .../vendor/i18n-0.1.1/test/simple_backend_test.rb | 34 +++++++++++++++++++- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.1/lib/i18n/backend/simple.rb b/activesupport/lib/active_support/vendor/i18n-0.1.1/lib/i18n/backend/simple.rb index b54164d..c09acd7 100644 --- a/activesupport/lib/active_support/vendor/i18n-0.1.1/lib/i18n/backend/simple.rb +++ b/activesupport/lib/active_support/vendor/i18n-0.1.1/lib/i18n/backend/simple.rb @@ -151,12 +151,7 @@ module I18n 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 @@ module I18n 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/activesupport/lib/active_support/vendor/i18n-0.1.1/test/simple_backend_test.rb b/activesupport/lib/active_support/vendor/i18n-0.1.1/test/simple_backend_test.rb index 8ba7036..4e27a98 100644 --- a/activesupport/lib/active_support/vendor/i18n-0.1.1/test/simple_backend_test.rb +++ b/activesupport/lib/active_support/vendor/i18n-0.1.1/test/simple_backend_test.rb @@ -253,6 +253,32 @@ class I18nSimpleBackendInterpolateTest < Test::Unit::TestCase 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 @@ class I18nSimpleBackendInterpolateTest < Test::Unit::TestCase 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 @@ -521,4 +553,4 @@ class I18nSimpleBackendReloadTranslationsTest < Test::Unit::TestCase @backend.reload! assert_equal @backend.initialized?, false end -end \ No newline at end of file +end -- 1.6.0.2