aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSven Fuchs <svenfuchs@artweb-design.de>2008-08-30 10:51:35 +0200
committerJeremy Kemper <jeremy@bitsweat.net>2008-08-30 02:02:33 -0700
commitbe4ae1f5264d6593e9dec479af4503c4bde2877e (patch)
treebed414370deab12be649f29fb51724ccde8d3827
parentefa6620a2a7b8ae7b42664ab81faef7df1368939 (diff)
downloadrails-be4ae1f5264d6593e9dec479af4503c4bde2877e.tar.gz
rails-be4ae1f5264d6593e9dec479af4503c4bde2877e.tar.bz2
rails-be4ae1f5264d6593e9dec479af4503c4bde2877e.zip
I18n: applied Luca Guidi's patch for better #interpolate performance
[#943 state:resolved] Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
-rw-r--r--activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb37
1 files changed, 20 insertions, 17 deletions
diff --git a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb
index 2e966a51be..da89b30c54 100644
--- a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb
+++ b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb
@@ -3,6 +3,9 @@ require 'strscan'
module I18n
module Backend
class Simple
+ INTERPOLATION_RESERVED_KEYS = %w(scope default)
+ MATCH = /(\\\\)?\{\{([^\}]+)\}\}/
+
# Accepts a list of paths to translation files. Loads translations from
# plain Ruby (*.rb) or YAML files (*.yml). See #load_rb and #load_yml
# for details.
@@ -114,29 +117,29 @@ module I18n
# the <tt>{{...}}</tt> key in a string (once for the string and once for the
# interpolation).
def interpolate(locale, string, values = {})
- return string if !string.is_a?(String)
+ return string unless string.is_a?(String)
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
- key = s.scan_until(/\}\}/)[0..-3]
- end_pos = s.pos - 1
+ original_encoding = string.encoding
+ string.force_encoding(Encoding::BINARY)
+ end
- raise ReservedInterpolationKey.new(key, string) if %w(scope default).include?(key)
- raise MissingInterpolationArgument.new(key, string) unless values.has_key? key.to_sym
+ result = string.gsub(MATCH) do
+ escaped, pattern, key = $1, $2, $2.to_sym
- s.string[start_pos..end_pos] = values[key.to_sym].to_s
- s.unscan
+ if escaped
+ pattern
+ elsif INTERPOLATION_RESERVED_KEYS.include?(pattern)
+ raise ReservedInterpolationKey.new(pattern, string)
+ elsif !values.include?(key)
+ raise MissingInterpolationArgument.new(pattern, string)
+ else
+ values[key].to_s
+ end
end
-
- result = s.string
+
result.force_encoding(original_encoding) if original_encoding
result
end