Permalink
Browse files

Remove unnecessary string encoding handling in the i18n simple backen…

…d which made the backend break on Ruby 1.9.
  • Loading branch information...
1 parent 7fa4211 commit 4c3a970783861a94f2e89f46714fb3434e4f4f8d @amatsuda amatsuda committed with Feb 27, 2009
Showing with 34 additions and 10 deletions.
  1. +1 −9 lib/i18n/backend/simple.rb
  2. +33 −1 test/simple_backend_test.rb
View
10 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
View
34 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
+end

0 comments on commit 4c3a970

Please sign in to comment.