95ffb6ab6fa87e76d550a8d1694f1830d9333a25
[packages/precise/mcollective.git] / lib / mcollective / vendor / i18n / lib / i18n / backend / simple.rb
1 module I18n
2   module Backend
3     # A simple backend that reads translations from YAML files and stores them in
4     # an in-memory hash. Relies on the Base backend.
5     #
6     # The implementation is provided by a Implementation module allowing to easily
7     # extend Simple backend's behavior by including modules. E.g.:
8     #
9     # module I18n::Backend::Pluralization
10     #   def pluralize(*args)
11     #     # extended pluralization logic
12     #     super
13     #   end
14     # end
15     #
16     # I18n::Backend::Simple.include(I18n::Backend::Pluralization)
17     class Simple
18       (class << self; self; end).class_eval { public :include }
19
20       module Implementation
21         include Base
22
23         def initialized?
24           @initialized ||= false
25         end
26
27         # Stores translations for the given locale in memory.
28         # This uses a deep merge for the translations hash, so existing
29         # translations will be overwritten by new ones only at the deepest
30         # level of the hash.
31         def store_translations(locale, data, options = {})
32           locale = locale.to_sym
33           translations[locale] ||= {}
34           data = data.deep_symbolize_keys
35           translations[locale].deep_merge!(data)
36         end
37
38         # Get available locales from the translations hash
39         def available_locales
40           init_translations unless initialized?
41           translations.inject([]) do |locales, (locale, data)|
42             locales << locale unless (data.keys - [:i18n]).empty?
43             locales
44           end
45         end
46
47         # Clean up translations hash and set initialized to false on reload!
48         def reload!
49           @initialized = false
50           @translations = nil
51           super
52         end
53
54       protected
55
56         def init_translations
57           load_translations
58           @initialized = true
59         end
60
61         def translations
62           @translations ||= {}
63         end
64
65         # Looks up a translation from the translations hash. Returns nil if
66         # eiher key is nil, or locale, scope or key do not exist as a key in the
67         # nested translations hash. Splits keys or scopes containing dots
68         # into multiple keys, i.e. <tt>currency.format</tt> is regarded the same as
69         # <tt>%w(currency format)</tt>.
70         def lookup(locale, key, scope = [], options = {})
71           init_translations unless initialized?
72           keys = I18n.normalize_keys(locale, key, scope, options[:separator])
73
74           keys.inject(translations) do |result, _key|
75             _key = _key.to_sym
76             return nil unless result.is_a?(Hash) && result.has_key?(_key)
77             result = result[_key]
78             result = resolve(locale, _key, result, options.merge(:scope => nil)) if result.is_a?(Symbol)
79             result
80           end
81         end
82       end
83
84       include Implementation
85     end
86   end
87 end