Module: I18n::Backend::KeyValue::Implementation

Includes:
Base, Flatten
Included in:
I18n::Backend::KeyValue
Defined in:
lib/i18n/backend/key_value.rb

Constant Summary

Constant Summary

Constants included from Transliterator

Transliterator::DEFAULT_REPLACEMENT_CHAR

Constants included from Flatten

Flatten::FLATTEN_SEPARATOR, Flatten::SEPARATOR_ESCAPE_CHAR

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Base

#exists?, #load_translations, #localize, #reload!, #translate

Methods included from Transliterator

get, #transliterate

Methods included from Flatten

escape_default_separator, #flatten_keys, #flatten_translations, #links, #normalize_flat_keys, normalize_flat_keys

Instance Attribute Details

#storeObject

Returns the value of attribute store



52
53
54
# File 'lib/i18n/backend/key_value.rb', line 52

def store
  @store
end

Instance Method Details

#available_localesObject



79
80
81
82
83
84
85
# File 'lib/i18n/backend/key_value.rb', line 79

def available_locales
  locales = @store.keys.map { |k| k =~ /\./; $` }
  locales.uniq!
  locales.compact!
  locales.map! { |k| k.to_sym }
  locales
end

#initialize(store, subtrees = true) ⇒ Object



56
57
58
# File 'lib/i18n/backend/key_value.rb', line 56

def initialize(store, subtrees=true)
  @store, @subtrees = store, subtrees
end

#store_translations(locale, data, options = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/i18n/backend/key_value.rb', line 60

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