module I18n::Backend::KeyValue::Implementation - i18n-0.6.11 Documentation

# module I18n::Backend::KeyValue::Implementation

### Attributes

store[RW]

### Public Class Methods

new(store, subtrees=true) click to toggle source

    # File lib/i18n/backend/key_value.rb, line 57
    def initialize(store, subtrees=true)
      @store, @subtrees = store, subtrees
    end

### Public Instance Methods

available_locales() click to toggle source

    # File lib/i18n/backend/key_value.rb, line 80
    def available_locales
      locales = @store.keys.map { |k| k =~ /\./; $` }
      locales.uniq!
      locales.compact!
      locales.map! { |k| k.to_sym }
      locales
    end

store_translations(locale, data, options = {}) click to toggle source

    # File lib/i18n/backend/key_value.rb, line 61
    def store_translations(locale, data, options = {})
      escape = options.fetch(:escape, true)
      flatten_translations(locale, data, escape, @subtrees).each do |key, value|
        key = "#{locale}.#{key}"

        case value
        when Hash
          if @subtrees && (old_value = @store[key])
            old_value = ActiveSupport::JSON.decode(old_value)
            value = old_value.deep_symbolize_keys.deep_merge!(value) if old_value.is_a?(Hash)
          end
        when Proc
          raise "Key-value stores cannot handle procs"
        end

        @store[key] = ActiveSupport::JSON.encode(value) unless value.is_a?(Symbol)
      end
    end

### Protected Instance Methods

lookup(locale, key, scope = [], options = {}) click to toggle source

    # File lib/i18n/backend/key_value.rb, line 90
    def lookup(locale, key, scope = [], options = {})
      key   = normalize_flat_keys(locale, key, scope, options[:separator])
      value = @store["#{locale}.#{key}"]
      value = ActiveSupport::JSON.decode(value) if value
      value.is_a?(Hash) ? value.deep_symbolize_keys : value
    end