Skip to content

Commit

Permalink
Remove unnecessary string encoding handling in the i18n simple backen…
Browse files Browse the repository at this point in the history
…d which made the backend break on Ruby 1.9.
  • Loading branch information
amatsuda authored and Sven Fuchs committed Feb 27, 2009
1 parent 7fa4211 commit 4c3a970
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
10 changes: 1 addition & 9 deletions lib/i18n/backend/simple.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
34 changes: 33 additions & 1 deletion test/simple_backend_test.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.