module I18n::Backend::KeyValue::Implementation
def available_locales
def available_locales locales = @store.keys.map { |k| k =~ /\./; $` } locales.uniq! locales.compact! locales.map! { |k| k.to_sym } locales end
def init_translations
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
def initialize(store, subtrees=true)
def initialize(store, subtrees=true) @store, @subtrees = store, subtrees end
def initialized?
def initialized? !@store.nil? end
def lookup(locale, key, scope = [], options = EMPTY_HASH)
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
def pluralize(locale, entry, count)
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
def store_translations(locale, data, options = EMPTY_HASH)
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
def subtrees?
def subtrees? @subtrees end
def translations
them into a hash such as the one returned from loading the
Queries the translations from the key-value store and converts
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