Permalink
Browse files

merged with jeremy's fork and fixed conflicts

  • Loading branch information...
2 parents 0f8f20a + fbafa42 commit bbf70642287e07010911255fe6ad03e92fc60ea2 @mseppae mseppae committed with Sven Fuchs Aug 25, 2008
Showing with 24 additions and 15 deletions.
  1. +2 −2 lib/i18n.rb
  2. +15 −11 lib/i18n/backend/simple.rb
  3. +7 −2 test/simple_backend_test.rb
View
@@ -183,8 +183,8 @@ def default_exception_handler(exception, locale, key, options)
# keys are Symbols.
def normalize_translation_keys(locale, key, scope)
keys = [locale] + Array(scope) + [key]
- keys = keys.map{|key| key.to_s.split(/\./) }
- keys.flatten.map{|key| key.to_sym}
+ keys = keys.map{|k| k.to_s.split(/\./) }
+ keys.flatten.map{|k| k.to_sym}
end
end
end
@@ -27,7 +27,7 @@ def store_translations(locale, data)
def translate(locale, key, options = {})
raise InvalidLocale.new(locale) if locale.nil?
- return key.map{|key| translate locale, key, options } if key.is_a? Array
+ return key.map{|k| translate locale, k, options } if key.is_a? Array
reserved = :scope, :default
count, scope, default = options.values_at(:count, *reserved)
@@ -74,7 +74,7 @@ def translations
def lookup(locale, key, scope = [])
return unless key
keys = I18n.send :normalize_translation_keys, locale, key, scope
- keys.inject(translations){|result, key| result[key.to_sym] or return nil }
+ keys.inject(translations){|result, k| result[k.to_sym] or return nil }
end
# Evaluates a default translation.
@@ -120,10 +120,13 @@ 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] == '\\'
start_pos = s.pos - 2
@@ -135,8 +138,11 @@ def interpolate(locale, string, values = {})
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
@@ -147,9 +153,7 @@ def load_file(filename)
type = File.extname(filename).tr('.', '').downcase
raise UnknownFileType.new(type, filename) unless respond_to? :"load_#{type}"
data = send :"load_#{type}", filename # TODO raise a meaningful exception if this does not yield a Hash
- data.each do |locale, data|
- merge_translations locale, data
- end
+ data.each{|locale, d| merge_translations locale, d }
end
# Loads a plain Ruby translations file. eval'ing the file must yield
@@ -1,3 +1,4 @@
+# encoding: utf-8
$:.unshift "lib"
require 'rubygems'
@@ -237,7 +238,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
@@ -377,7 +382,7 @@ def setup
end
def teardown
- ENV['TZ'] = @old_timezone
+ @old_timezone ? ENV['TZ'] = @old_timezone : ENV.delete('TZ')
end
def test_translate_given_the_short_format_it_uses_it

0 comments on commit bbf7064

Please sign in to comment.