Module: I18n::Backend::Flatten

### Home Classes Methods

### In Files

- lib/i18n/backend/flatten.rb

### Methods

- ::normalize_flat_keys
- #flatten_keys
- #flatten_translations
- #links
- #normalize_flat_keys
- #resolve_link
- #store_link

### Class/Module Index

Quicksearch

- I18n
- I18n::ArgumentError
- I18n::Backend
- I18n::Backend::Base
- I18n::Backend::Cache
- I18n::Backend::Cascade
- I18n::Backend::Chain
- I18n::Backend::Chain::Implementation
- I18n::Backend::Fallbacks
- I18n::Backend::Flatten
- I18n::Backend::Gettext
- I18n::Backend::Gettext::PoData
- I18n::Backend::InterpolationCompiler
- I18n::Backend::InterpolationCompiler::Compiler
- I18n::Backend::KeyValue
- I18n::Backend::KeyValue::Implementation
- I18n::Backend::Memoize
- I18n::Backend::Metadata
- I18n::Backend::Pluralization
- I18n::Backend::Simple
- I18n::Backend::Simple::Implementation
- I18n::Config
- I18n::ExceptionHandler
- I18n::Gettext
- I18n::Gettext::Helpers
- I18n::InvalidLocale
- I18n::InvalidLocaleData
- I18n::InvalidPluralizationData
- I18n::Locale
- I18n::Locale::Fallbacks
- I18n::Locale::Tag
- I18n::Locale::Tag::Parents
- I18n::Locale::Tag::Rfc4646
- I18n::Locale::Tag::Rfc4646::Parser
- I18n::Locale::Tag::Simple
- I18n::MissingInterpolationArgument
- I18n::MissingTranslation
- I18n::MissingTranslation::Base
- I18n::MissingTranslationData
- I18n::ReservedInterpolationKey
- I18n::Tests
- I18n::Tests::Basics
- I18n::Tests::Defaults
- I18n::Tests::Link
- I18n::Tests::Localization
- I18n::Tests::Lookup
- I18n::Tests::Pluralization
- I18n::Tests::Procs
- I18n::UnknownFileType
- GetText
- GetText::PoParser
- Hash
- Kernel

No matching classes.

# I18n::Backend::Flatten

This module contains several helpers to assist flattening translations. You may want to flatten translations for:

    1) speed up lookups, as in the Memoize backend;
    2) In case you want to store translations in a data store, as in ActiveRecord backend;

You can check both backends above for some examples. This module also keeps all links in a hash so they can be properly resolved when flattened.

### Constants

FLATTEN_SEPARATOR
SEPARATOR_ESCAPE_CHAR

### Public Class Methods

normalize_flat_keys(locale, key, scope, separator) click to toggle source

normalize_keys the flatten way. This method is significantly faster and creates way less objects than the one at I18n.normalize_keys. It also handles escaping the translation keys.

    # File lib/i18n/backend/flatten.rb, line 18
    def self.normalize_flat_keys(locale, key, scope, separator)
      keys = [scope, key].flatten.compact
      separator ||= I18n.default_separator

      if separator != FLATTEN_SEPARATOR
        keys.map! do |k|
          k.to_s.tr("#{FLATTEN_SEPARATOR}#{separator}",
            "#{SEPARATOR_ESCAPE_CHAR}#{FLATTEN_SEPARATOR}")
        end
      end

      keys.join(".")
    end

### Public Instance Methods

flatten_keys(hash, escape, prev_key=nil, &block) click to toggle source

Flatten keys for nested Hashes by chaining up keys:

    >> { "a" => { "b" => { "c" => "d", "e" => "f" }, "g" => "h" }, "i" => "j"}.wind
    => { "a.b.c" => "d", "a.b.e" => "f", "a.g" => "h", "i" => "j" }

    # File lib/i18n/backend/flatten.rb, line 54
    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

flatten_translations(locale, data, escape, subtree) click to toggle source

Receives a hash of translations (where the key is a locale and the value is another hash) and return a hash with all translations flattened.

Nested hashes are included in the flattened hash just if subtree is true and Symbols are automatically stored as links.

    # File lib/i18n/backend/flatten.rb, line 69
    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

links() click to toggle source

Store flattened links.

    # File lib/i18n/backend/flatten.rb, line 45
    def links
      @links ||= Hash.new { |h,k| h[k] = {} }
    end

normalize_flat_keys(locale, key, scope, separator) click to toggle source

Shortcut to I18n::Backend::Flatten.normalize_flat_keys and then resolve_links.

    # File lib/i18n/backend/flatten.rb, line 39
    def normalize_flat_keys(locale, key, scope, separator)
      key = I18n::Backend::Flatten.normalize_flat_keys(locale, key, scope, separator)
      resolve_link(locale, key)
    end

### Protected Instance Methods

resolve_link(locale, key) click to toggle source

    # File lib/i18n/backend/flatten.rb, line 88
    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

store_link(locale, key, link) click to toggle source

    # File lib/i18n/backend/flatten.rb, line 84
    def store_link(locale, key, link)
      links[locale.to_sym][key.to_s] = link.to_s
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.