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

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

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

#eager_load!, #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.


71
72
73
# File 'lib/i18n/backend/key_value.rb', line 71

def store
  @store
end

Instance Method Details

#available_localesObject


102
103
104
105
106
107
108
# 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

#initialize(store, subtrees = true) ⇒ Object


75
76
77
# File 'lib/i18n/backend/key_value.rb', line 75

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

#initialized?Boolean

Returns:

  • (Boolean)

79
80
81
# File 'lib/i18n/backend/key_value.rb', line 79

def initialized?
  !@store.nil?
end

#store_translations(locale, data, options = EMPTY_HASH) ⇒ Object


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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