Skip to content

Commit

Permalink
interpolate deals with non-ASCII string encodings
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy committed Aug 24, 2008
1 parent abd8792 commit d84a3f3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 32 deletions.
24 changes: 15 additions & 9 deletions lib/i18n/backend/simple.rb
Expand Up @@ -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
Expand Down
28 changes: 5 additions & 23 deletions test/simple_backend_test.rb
Expand Up @@ -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
Expand Down Expand Up @@ -460,25 +464,3 @@ def test_load_translations_loads_from_different_file_formats
assert_equal expected, result
end
end






















0 comments on commit d84a3f3

Please sign in to comment.