1 # I18n translation metadata is useful when you want to access information
2 # about how a translation was looked up, pluralized or interpolated in
5 # msg = I18n.t(:message, :default => 'Hi!', :scope => :foo)
6 # msg.translation_metadata
7 # # => { :key => :message, :scope => :foo, :default => 'Hi!' }
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.
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:
15 # I18n::Backend::Simple.include(I18n::Backend::Metadata)
23 def translation_metadata
24 @translation_metadata ||= {}
27 def translation_metadata=(translation_metadata)
28 @translation_metadata = translation_metadata
30 end unless Object.method_defined?(:translation_metadata)
34 def translate(locale, key, options = {})
38 :scope => options[:scope],
39 :default => options[:default],
40 :separator => options[:separator],
41 :values => options.reject { |name, value| RESERVED_KEYS.include?(name) }
43 with_metadata(metadata) { super }
46 def interpolate(locale, entry, values = {})
47 metadata = entry.translation_metadata.merge(:original => entry)
48 with_metadata(metadata) { super }
51 def pluralize(locale, entry, count)
52 with_metadata(:count => count) { super }
57 def with_metadata(metadata, &block)
59 result.translation_metadata = result.translation_metadata.merge(metadata) if result