module I18n::Backend::KeyValue::Implementation

Attributes

Public Class Methods

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

Public Instance Methods

# File lib/i18n/backend/key_value.rb, line 102
def available_locales
  locales = @store.keys.map { |k| k =~ /\./; $` }
  locales.uniq!
  locales.compact!
  locales.map! { |k| k.to_sym }
  locales
end
# File lib/i18n/backend/key_value.rb, line 79
def initialized?
  !@store.nil?
end
# File lib/i18n/backend/key_value.rb, line 83
def store_translations(locale, data, options = EMPTY_HASH)
  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 = JSON.decode(old_value)
        value = Utils.deep_merge!(Utils.deep_symbolize_keys(old_value), value) if old_value.is_a?(Hash)
      end
    when Proc
      raise "Key-value stores cannot handle procs"
    end

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

Protected Instance Methods

# File lib/i18n/backend/key_value.rb, line 124
def init_translations
  # NO OP
  # This call made also inside Simple Backend and accessed by
  # other plugins like I18n-js and babilu and
  # to use it along with the Chain backend we need to
  # provide a uniform API even for protected methods :S
end
# File lib/i18n/backend/key_value.rb, line 136
def lookup(locale, key, scope = [], options = EMPTY_HASH)
  key   = normalize_flat_keys(locale, key, scope, options[:separator])
  value = @store["#{locale}.#{key}"]
  value = JSON.decode(value) if value

  if value.is_a?(Hash)
    Utils.deep_symbolize_keys(value)
  elsif !value.nil?
    value
  elsif !@subtrees
    SubtreeProxy.new("#{locale}.#{key}", @store)
  end
end
Calls superclass method
# File lib/i18n/backend/key_value.rb, line 150
def pluralize(locale, entry, count)
  if subtrees?
    super
  else
    return entry unless entry.is_a?(Hash)
    key = pluralization_key(entry, count)
    entry[key]
  end
end
# File lib/i18n/backend/key_value.rb, line 132
def subtrees?
  @subtrees
end

Queries the translations from the key-value store and converts them into a hash such as the one returned from loading the haml files

# File lib/i18n/backend/key_value.rb, line 115
def translations
  @translations = Utils.deep_symbolize_keys(@store.keys.clone.map do |main_key|
    main_value = JSON.decode(@store[main_key])
    main_key.to_s.split(".").reverse.inject(main_value) do |value, key|
      {key.to_sym => value}
    end
  end.inject{|hash, elem| Utils.deep_merge!(hash, elem)})
end