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