From d84a3f3f55543c084d5dc5d1fed613b8df148789 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 23 Aug 2008 17:56:18 -0700 Subject: [PATCH] interpolate deals with non-ASCII string encodings --- lib/i18n/backend/simple.rb | 24 +++++++++++++++--------- test/simple_backend_test.rb | 28 +++++----------------------- 2 files changed, 20 insertions(+), 32 deletions(-) diff --git a/lib/i18n/backend/simple.rb b/lib/i18n/backend/simple.rb index 1362b365..917646ff 100644 --- a/lib/i18n/backend/simple.rb +++ b/lib/i18n/backend/simple.rb @@ -119,25 +119,31 @@ def pluralize(locale, entry, count) def interpolate(locale, string, values = {}) return string if !string.is_a?(String) - map = {'%d' => '{{count}}', '%s' => '{{value}}'} # TODO deprecate this? - string.gsub!(/#{map.keys.join('|')}/){|key| map[key]} - - s = StringScanner.new string.dup + string = string.gsub(/%d/, '{{count}}').gsub(/%s/, '{{value}}') + if string.respond_to?(:force_encoding) + original_encoding = string.encoding + string.force_encoding(Encoding::BINARY) + end + + s = StringScanner.new(string) while s.skip_until(/\{\{/) - s.string[s.pos - 3, 1] = '' and next if s.pre_match[-1, 1] == '\\' + s.string[s.pos - 3, 1] = '' and next if s.pre_match[-1, 1] == '\\' start_pos = s.pos - 2 key = s.scan_until(/\}\}/)[0..-3] - end_pos = s.pos - 1 + end_pos = s.pos - 1 raise ReservedInterpolationKey.new(key, string) if %w(scope default).include?(key) raise MissingInterpolationArgument.new(key, string) unless values.has_key? key.to_sym s.string[start_pos..end_pos] = values[key.to_sym].to_s s.unscan - end - s.string + end + + result = s.string + result.force_encoding(original_encoding) if original_encoding + result end - + # Loads a single translations file by delegating to #load_rb or # #load_yml depending on the file extension and directly merges the # data to the existing translations. Raises I18n::UnknownFileType diff --git a/test/simple_backend_test.rb b/test/simple_backend_test.rb index cad2efb7..8e7a19d9 100644 --- a/test/simple_backend_test.rb +++ b/test/simple_backend_test.rb @@ -230,7 +230,11 @@ class I18nSimpleBackendInterpolateTest < Test::Unit::TestCase def test_interpolate_given_a_value_hash_interpolates_the_values_to_the_string assert_equal 'Hi David!', @backend.send(:interpolate, nil, 'Hi {{name}}!', :name => 'David') end - + + 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_nil_as_a_string_returns_nil assert_nil @backend.send(:interpolate, nil, nil, :name => 'David') end @@ -460,25 +464,3 @@ def test_load_translations_loads_from_different_file_formats assert_equal expected, result end end - - - - - - - - - - - - - - - - - - - - - -