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

Overview
Module Structure
Class Hierarchy
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: I18n#4
  module: Backend#5
  class: ActiveRecord#6
includes
  Implementation ( Unknown-Module )
inherits from
  Object ( Builtin-Module )
  module: Implementation#11
includes
  Base ( I18n::Backend )
  Flatten ( I18n::Backend )
has properties
method: available_locales #14
method: store_translations / 3 #22
method: lookup / 4 #32
method: expand_keys / 1 #51

Class Hierarchy

Code

   1  require 'i18n/backend/base'
   2  require 'i18n/backend/active_record/translation'
   3 
   4  module I18n
   5    module Backend
   6      class ActiveRecord
   7        autoload :Missing,     'i18n/backend/active_record/missing'
   8        autoload :StoreProcs,  'i18n/backend/active_record/store_procs'
   9        autoload :Translation, 'i18n/backend/active_record/translation'
  10 
  11        module Implementation
  12          include Base, Flatten
  13 
  14          def available_locales
  15            begin
  16              Translation.available_locales
  17            rescue ::ActiveRecord::StatementInvalid
  18              []
  19            end
  20          end
  21 
  22          def store_translations(locale, data, options = {})
  23            escape = options.fetch(:escape, true)
  24            flatten_translations(locale, data, escape, false).each do |key, value|
  25              Translation.locale(locale).lookup(expand_keys(key)).delete_all
  26              Translation.create(:locale => locale.to_s, :key => key.to_s, :value => value)
  27            end
  28          end
  29 
  30        protected
  31 
  32          def lookup(locale, key, scope = [], options = {})
  33            key = normalize_flat_keys(locale, key, scope, options[:separator])
  34            result = Translation.locale(locale).lookup(key).all
  35 
  36            if result.empty?
  37              nil
  38            elsif result.first.key == key
  39              result.first.value
  40            else
  41              chop_range = (key.size + FLATTEN_SEPARATOR.size)..-1
  42              result = result.inject({}) do |hash, r|
  43                hash[r.key.slice(chop_range)] = r.value
  44                hash
  45              end
  46              result.deep_symbolize_keys
  47            end
  48          end
  49 
  50          # For a key :'foo.bar.baz' return ['foo', 'foo.bar', 'foo.bar.baz']
  51          def expand_keys(key)
  52            key.to_s.split(FLATTEN_SEPARATOR).inject([]) do |keys, key|
  53              keys << [keys.last, key].compact.join(FLATTEN_SEPARATOR)
  54            end
  55          end
  56        end
  57 
  58        include Implementation
  59      end
  60    end
  61  end