mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
I18n: applied Luca Guidi's patch for better #interpolate performance
[#943 state:resolved] Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
This commit is contained in:
parent
efa6620a2a
commit
be4ae1f526
1 changed files with 22 additions and 19 deletions
|
@ -3,6 +3,9 @@ require 'strscan'
|
||||||
module I18n
|
module I18n
|
||||||
module Backend
|
module Backend
|
||||||
class Simple
|
class Simple
|
||||||
|
INTERPOLATION_RESERVED_KEYS = %w(scope default)
|
||||||
|
MATCH = /(\\\\)?\{\{([^\}]+)\}\}/
|
||||||
|
|
||||||
# Accepts a list of paths to translation files. Loads translations from
|
# Accepts a list of paths to translation files. Loads translations from
|
||||||
# plain Ruby (*.rb) or YAML files (*.yml). See #load_rb and #load_yml
|
# plain Ruby (*.rb) or YAML files (*.yml). See #load_rb and #load_yml
|
||||||
# for details.
|
# for details.
|
||||||
|
@ -114,29 +117,29 @@ module I18n
|
||||||
# the <tt>{{...}}</tt> key in a string (once for the string and once for the
|
# the <tt>{{...}}</tt> key in a string (once for the string and once for the
|
||||||
# interpolation).
|
# interpolation).
|
||||||
def interpolate(locale, string, values = {})
|
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}}')
|
string = string.gsub(/%d/, '{{count}}').gsub(/%s/, '{{value}}')
|
||||||
|
|
||||||
if string.respond_to?(:force_encoding)
|
if string.respond_to?(:force_encoding)
|
||||||
original_encoding = string.encoding
|
original_encoding = string.encoding
|
||||||
string.force_encoding(Encoding::BINARY)
|
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
|
|
||||||
|
|
||||||
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
|
end
|
||||||
|
|
||||||
result = s.string
|
result = string.gsub(MATCH) do
|
||||||
|
escaped, pattern, key = $1, $2, $2.to_sym
|
||||||
|
|
||||||
|
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.force_encoding(original_encoding) if original_encoding
|
result.force_encoding(original_encoding) if original_encoding
|
||||||
result
|
result
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue