File: active_support/vendor/i18n-0.4.1/i18n/backend/metadata.rb

Overview
Module Structure
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: I18n#17
  module: Backend#18
  module: Metadata#19
has properties
module method: included / 1 #21
module method: translation_metadata #23
module method: translation_metadata= / 1 #27
method: translate / 3 #34
method: interpolate / 3 #46
method: pluralize / 3 #51
method: with_metadata / 2 #57

Code

   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.send(: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| Base::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