c34b797c969827767a44608b3090fe1249462020
[packages/precise/mcollective.git] / lib / mcollective / vendor / i18n / lib / i18n / backend / key_value.rb
1 require 'i18n/backend/base'
2 require 'active_support/json'
3 require 'active_support/ordered_hash' # active_support/json/encoding uses ActiveSupport::OrderedHash but does not require it
4
5 module I18n
6   module Backend
7     # This is a basic backend for key value stores. It receives on
8     # initialization the store, which should respond to three methods:
9     #
10     # * store#[](key)         - Used to get a value
11     # * store#[]=(key, value) - Used to set a value
12     # * store#keys            - Used to get all keys
13     #
14     # Since these stores only supports string, all values are converted
15     # to JSON before being stored, allowing it to also store booleans,
16     # hashes and arrays. However, this store does not support Procs.
17     #
18     # As the ActiveRecord backend, Symbols are just supported when loading
19     # translations from the filesystem or through explicit store translations.
20     #
21     # Also, avoid calling I18n.available_locales since it's a somehow
22     # expensive operation in most stores.
23     #
24     # == Example
25     #
26     # To setup I18n to use TokyoCabinet in memory is quite straightforward:
27     #
28     #   require 'rufus/tokyo/cabinet' # gem install rufus-tokyo
29     #   I18n.backend = I18n::Backend::KeyValue.new(Rufus::Tokyo::Cabinet.new('*'))
30     #
31     # == Performance
32     #
33     # You may make this backend even faster by including the Memoize module.
34     # However, notice that you should properly clear the cache if you change
35     # values directly in the key-store.
36     #
37     # == Subtrees
38     #
39     # In most backends, you are allowed to retrieve part of a translation tree:
40     #
41     #   I18n.backend.store_translations :en, :foo => { :bar => :baz }
42     #   I18n.t "foo" #=> { :bar => :baz }
43     #
44     # This backend supports this feature by default, but it slows down the storage
45     # of new data considerably and makes hard to delete entries. That said, you are
46     # allowed to disable the storage of subtrees on initialization:
47     #
48     #   I18n::Backend::KeyValue.new(@store, false)
49     #
50     # This is useful if you are using a KeyValue backend chained to a Simple backend.
51     class KeyValue
52       module Implementation
53         attr_accessor :store
54
55         include Base, Flatten
56
57         def initialize(store, subtrees=true)
58           @store, @subtrees = store, subtrees
59         end
60
61         def store_translations(locale, data, options = {})
62           escape = options.fetch(:escape, true)
63           flatten_translations(locale, data, escape, @subtrees).each do |key, value|
64             key = "#{locale}.#{key}"
65
66             case value
67             when Hash
68               if @subtrees && (old_value = @store[key])
69                 old_value = ActiveSupport::JSON.decode(old_value)
70                 value = old_value.deep_symbolize_keys.deep_merge!(value) if old_value.is_a?(Hash)
71               end
72             when Proc
73               raise "Key-value stores cannot handle procs"
74             end
75
76             @store[key] = ActiveSupport::JSON.encode([value]) unless value.is_a?(Symbol)
77           end
78         end
79
80         def available_locales
81           locales = @store.keys.map { |k| k =~ /\./; $` }
82           locales.uniq!
83           locales.compact!
84           locales.map! { |k| k.to_sym }
85           locales
86         end
87
88       protected
89
90         def lookup(locale, key, scope = [], options = {})
91           key   = normalize_flat_keys(locale, key, scope, options[:separator])
92           value = @store["#{locale}.#{key}"]
93           value = ActiveSupport::JSON.decode(value)[0] if value
94           value.is_a?(Hash) ? value.deep_symbolize_keys : value
95         end
96       end
97
98       include Implementation
99     end
100   end
101 end