module I18n::Backend::Flatten
def self.escape_default_separator(key) #:nodoc:
Receives a string and escape the default separator.
def self.escape_default_separator(key) #:nodoc: key.to_s.tr(FLATTEN_SEPARATOR, SEPARATOR_ESCAPE_CHAR) end
def self.normalize_flat_keys(locale, key, scope, separator)
and creates way less objects than the one at I18n.normalize_keys.
normalize_keys the flatten way. This method is significantly faster
def self.normalize_flat_keys(locale, key, scope, separator) keys = [scope, key] keys.flatten! keys.compact! separator ||= I18n.default_separator if separator != FLATTEN_SEPARATOR from_str = "#{FLATTEN_SEPARATOR}#{separator}" to_str = "#{SEPARATOR_ESCAPE_CHAR}#{FLATTEN_SEPARATOR}" keys.map! { |k| k.to_s.tr from_str, to_str } end keys.join(".") end
def escape_default_separator(key) #:nodoc:
def escape_default_separator(key) #:nodoc: I18n::Backend::Flatten.escape_default_separator(key) end
def find_link(locale, key) #:nodoc:
def find_link(locale, key) #:nodoc: links[locale].each_pair do |from, to| return [from, to] if key[0, from.length] == from end && nil end
def flatten_keys(hash, escape, prev_key=nil, &block)
=> { "a.b.c" => "d", "a.b.e" => "f", "a.g" => "h", "i" => "j" }
>> { "a" => { "b" => { "c" => "d", "e" => "f" }, "g" => "h" }, "i" => "j"}.wind
Flatten keys for nested Hashes by chaining up keys:
def flatten_keys(hash, escape, prev_key=nil, &block) hash.each_pair do |key, value| key = escape_default_separator(key) if escape curr_key = [prev_key, key].compact.join(FLATTEN_SEPARATOR).to_sym yield curr_key, value flatten_keys(value, escape, curr_key, &block) if value.is_a?(Hash) end end
def flatten_translations(locale, data, escape, subtree)
Nested hashes are included in the flattened hash just if subtree
translations flattened.
the value is another hash) and return a hash with all
Receives a hash of translations (where the key is a locale and
def flatten_translations(locale, data, escape, subtree) hash = {} flatten_keys(data, escape) do |key, value| if value.is_a?(Hash) hash[key] = value if subtree else store_link(locale, key, value) if value.is_a?(Symbol) hash[key] = value end end hash end
def links
def links @links ||= I18n.new_double_nested_cache end
def normalize_flat_keys(locale, key, scope, separator)
Shortcut to I18n::Backend::Flatten.normalize_flat_keys
def normalize_flat_keys(locale, key, scope, separator) key = I18n::Backend::Flatten.normalize_flat_keys(locale, key, scope, separator) resolve_link(locale, key) end
def resolve_link(locale, key)
def resolve_link(locale, key) key, locale = key.to_s, locale.to_sym links = self.links[locale] if links.key?(key) links[key] elsif link = find_link(locale, key) store_link(locale, key, key.gsub(*link)) else key end end
def store_link(locale, key, link)
def store_link(locale, key, link) links[locale.to_sym][key.to_s] = link.to_s end