52c0a295b9958536c23cf5b5ebe807698f329f03
[packages/precise/mcollective.git] / lib / mcollective / vendor / i18n / lib / i18n / backend / metadata.rb
1 # I18n translation metadata is useful when you want to access information
2 # about how a translation was looked up, pluralized or interpolated in
3 # your application.
4 #
5 #   msg = I18n.t(:message, :default => 'Hi!', :scope => :foo)
6 #   msg.translation_metadata
7 #   # => { :key => :message, :scope => :foo, :default => 'Hi!' }
8 #
9 # If a :count option was passed to #translate it will be set to the metadata.
10 # Likewise, if any interpolation variables were passed they will also be set.
11 #
12 # To enable translation metadata you can simply include the Metadata module
13 # into the Simple backend class - or whatever other backend you are using:
14 #
15 #   I18n::Backend::Simple.include(I18n::Backend::Metadata)
16 #
17 module I18n
18   module Backend
19     module Metadata
20       class << self
21         def included(base)
22           Object.class_eval do
23             def translation_metadata
24               @translation_metadata ||= {}
25             end
26
27             def translation_metadata=(translation_metadata)
28               @translation_metadata = translation_metadata
29             end
30           end unless Object.method_defined?(:translation_metadata)
31         end
32       end
33
34       def translate(locale, key, options = {})
35         metadata = {
36           :locale    => locale,
37           :key       => key,
38           :scope     => options[:scope],
39           :default   => options[:default],
40           :separator => options[:separator],
41           :values    => options.reject { |name, value| RESERVED_KEYS.include?(name) }
42         }
43         with_metadata(metadata) { super }
44       end
45
46       def interpolate(locale, entry, values = {})
47         metadata = entry.translation_metadata.merge(:original => entry)
48         with_metadata(metadata) { super }
49       end
50
51       def pluralize(locale, entry, count)
52         with_metadata(:count => count) { super }
53       end
54
55       protected
56
57         def with_metadata(metadata, &block)
58           result = yield
59           result.translation_metadata = result.translation_metadata.merge(metadata) if result
60           result
61         end
62
63     end
64   end
65 end