Skip to content

Commit

Permalink
Reduce more the objects creation on key normalization.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Apr 28, 2010
1 parent be41e9e commit 6425f55
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions lib/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,12 @@ def localize(object, options = {})
# keys are Symbols.
def normalize_keys(locale, key, scope, separator = nil)
separator ||= I18n.default_separator
normalize_key(locale, separator) +
normalize_key(scope, separator) +
normalize_key(key, separator)

keys = []
keys.concat normalize_key(locale, separator)
keys.concat normalize_key(scope, separator)
keys.concat normalize_key(key, separator)
keys
end

# making these private until Ruby 1.9.2 can send to protected methods again
Expand Down Expand Up @@ -308,28 +311,20 @@ def normalize_translation_keys(locale, key, scope, separator = nil)
end

def normalize_key(key, separator)
normalized_key_cache(separator)[key] ||=
normalized_key_cache[separator][key] ||=
case key
when Array
key.map { |k| normalize_key(k, separator) }.flatten
when nil
[]
else
key = key.to_s
if key == ''
[]
elsif key.include?(separator)
keys = key.split(separator) - ['']
keys.map { |k| k.to_sym }
else
[key.to_sym]
end
keys = key.to_s.split(separator)
keys.delete('')
keys.map!{ |k| k.to_sym }
keys
end
end

def normalized_key_cache(separator)
def normalized_key_cache
@normalized_key_cache ||= Hash.new { |h,k| h[k] = {} }
@normalized_key_cache[separator]
end
end
end

0 comments on commit 6425f55

Please sign in to comment.