File: active_support/vendor/i18n-0.4.1/i18n/backend/active_record/missing.rb

Overview
Module Structure
Class Hierarchy
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: I18n#32
  module: Backend#33
  class: ActiveRecord#34
includes
  Implementation ( Unknown-Module )
inherits from
  Object ( Builtin-Module )
  module: Missing#35
has properties
method: store_default_translations / 3 #36
method: store_default_translation / 3 #50
method: translate / 3 #56

Class Hierarchy

Code

   1  #  This extension stores translation stub records for missing translations to
   2  #  the database.
   3  #
   4  #  This is useful if you have a web based translation tool. It will populate
   5  #  the database with untranslated keys as the application is being used. A
   6  #  translator can then go through these and add missing translations.
   7  #
   8  #  Example usage:
   9  #
  10  #     I18n::Backend::Chain.send(:include, I18n::Backend::ActiveRecord::Missing)
  11  #     I18n.backend = I18nChainBackend.new(I18n::Backend::ActiveRecord.new, I18n::Backend::Simple.new)
  12  #
  13  #  Stub records for pluralizations will also be created for each key defined
  14  #  in i18n.plural.keys.
  15  #
  16  #  For example:
  17  #
  18  #    # en.yml
  19  #    en:
  20  #      i18n:
  21  #        plural:
  22  #          keys: [:zero, :one, :other]
  23  #
  24  #    # pl.yml
  25  #    pl:
  26  #      i18n:
  27  #        plural:
  28  #          keys: [:zero, :one, :few, :other]
  29  #
  30  #  It will also persist interpolation keys in Translation#interpolations so
  31  #  translators will be able to review and use them.
  32  module I18n
  33    module Backend
  34      class ActiveRecord
  35        module Missing
  36          def store_default_translations(locale, key, options = {})
  37            count, scope, default, separator = options.values_at(:count, *Base::RESERVED_KEYS)
  38            separator ||= I18n.default_separator
  39 
  40            keys = I18n.normalize_keys(locale, key, scope, separator)[1..-1]
  41            key = keys.join(separator || I18n.default_separator)
  42 
  43            unless ActiveRecord::Translation.locale(locale).lookup(key).exists?
  44              interpolations = options.reject { |name, value| Base::RESERVED_KEYS.include?(name) }.keys
  45              keys = count ? I18n.t('i18n.plural.keys', :locale => locale).map { |k| [key, k].join(separator) } : [key]
  46              keys.each { |key| store_default_translation(locale, key, interpolations) }
  47            end
  48          end
  49 
  50          def store_default_translation(locale, key, interpolations)
  51            translation = ActiveRecord::Translation.new :locale => locale.to_s, :key => key
  52            translation.interpolations = interpolations
  53            translation.save
  54          end
  55 
  56          def translate(locale, key, options = {})
  57            super
  58          rescue I18n::MissingTranslationData => e
  59            self.store_default_translations(locale, key, options)
  60            raise e
  61          end
  62        end
  63      end
  64    end
  65  end