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 77
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 104
def available_locales
  locales = @store.keys.map { |k| k =~ /\./; $` }
  locales.uniq!
  locales.compact!
  locales.map! { |k| k.to_sym }
  locales
end
initialized?() click to toggle source
# File lib/i18n/backend/key_value.rb, line 81
def initialized?
  !@store.nil?
end
store_translations(locale, data, options = EMPTY_HASH) click to toggle source
# File lib/i18n/backend/key_value.rb, line 85
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 = 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] = JSON.encode(value) unless value.is_a?(Symbol)
  end
end

Protected Instance Methods

init_translations() click to toggle source
# File lib/i18n/backend/key_value.rb, line 126
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
lookup(locale, key, scope = [], options = EMPTY_HASH) click to toggle source
# File lib/i18n/backend/key_value.rb, line 138
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)
    value.deep_symbolize_keys
  elsif !value.nil?
    value
  elsif !@subtrees
    SubtreeProxy.new("#{locale}.#{key}", @store)
  end
end
pluralize(locale, entry, count) click to toggle source
Calls superclass method
# File lib/i18n/backend/key_value.rb, line 152
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
subtrees?() click to toggle source
# File lib/i18n/backend/key_value.rb, line 134
def subtrees?
  @subtrees
end
translations() click to toggle source

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 117
def translations
  @translations = @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| hash.deep_merge!(elem)}.deep_symbolize_keys
end