1 # Memoize module simply memoizes the values returned by lookup using
2 # a flat hash and can tremendously speed up the lookup process in a backend.
4 # To enable it you can simply include the Memoize module to your backend:
6 # I18n::Backend::Simple.include(I18n::Backend::Memoize)
8 # Notice that it's the responsibility of the backend to define whenever the
9 # cache should be cleaned.
14 @memoized_locales ||= super
17 def store_translations(locale, data, options = {})
18 reset_memoizations!(locale)
29 def lookup(locale, key, scope = nil, options = {})
30 flat_key = I18n::Backend::Flatten.normalize_flat_keys(locale,
31 key, scope, options[:separator]).to_sym
32 flat_hash = memoized_lookup[locale.to_sym]
33 flat_hash.key?(flat_key) ? flat_hash[flat_key] : (flat_hash[flat_key] = super)
37 @memoized_lookup ||= Hash.new { |h, k| h[k] = {} }
40 def reset_memoizations!(locale=nil)
41 @memoized_locales = nil
42 (locale ? memoized_lookup[locale.to_sym] : memoized_lookup).clear