Sha256: 23b685ea75f3e7ba091076e1a99c851641425d2b20dc0f2425cb3616b1ffad25

Contents?: true

Size: 1.86 KB

Versions: 48

Compression:

Stored size: 1.86 KB

Contents

require 'i18n/backend/base'
require 'i18n/backend/active_record/translation'

module I18n
  module Backend
    class ActiveRecord
      autoload :Missing,     'i18n/backend/active_record/missing'
      autoload :StoreProcs,  'i18n/backend/active_record/store_procs'
      autoload :Translation, 'i18n/backend/active_record/translation'

      module Implementation
        include Base, Flatten

        def available_locales
          begin
            Translation.available_locales
          rescue ::ActiveRecord::StatementInvalid
            []
          end
        end

        def store_translations(locale, data, options = {})
          escape = options.fetch(:escape, true)
          flatten_translations(locale, data, escape, false).each do |key, value|
            Translation.locale(locale).lookup(expand_keys(key)).delete_all
            Translation.create(:locale => locale.to_s, :key => key.to_s, :value => value)
          end
        end

      protected

        def lookup(locale, key, scope = [], options = {})
          key = normalize_flat_keys(locale, key, scope, options[:separator])
          result = Translation.locale(locale).lookup(key).all

          if result.empty?
            nil
          elsif result.first.key == key
            result.first.value
          else
            chop_range = (key.size + FLATTEN_SEPARATOR.size)..-1
            result = result.inject({}) do |hash, r|
              hash[r.key.slice(chop_range)] = r.value
              hash
            end
            result.deep_symbolize_keys
          end
        end

        # For a key :'foo.bar.baz' return ['foo', 'foo.bar', 'foo.bar.baz']
        def expand_keys(key)
          key.to_s.split(FLATTEN_SEPARATOR).inject([]) do |keys, key|
            keys << [keys.last, key].compact.join(FLATTEN_SEPARATOR)
          end
        end
      end

      include Implementation
    end
  end
end

Version data entries

48 entries across 48 versions & 6 rubygems

Version Path
depengine-0.0.31 etc/isolate/ruby-1.8/gems/i18n-0.4.2/lib/i18n/backend/active_record.rb
depengine-0.0.30 etc/isolate/ruby-1.8/gems/i18n-0.4.2/lib/i18n/backend/active_record.rb
depengine-0.0.29 etc/isolate/ruby-1.8/gems/i18n-0.4.2/lib/i18n/backend/active_record.rb
depengine-0.0.28 etc/isolate/ruby-1.8/gems/i18n-0.4.2/lib/i18n/backend/active_record.rb
depengine-0.0.27 etc/isolate/ruby-1.8/gems/i18n-0.4.2/lib/i18n/backend/active_record.rb
depengine-0.0.26 etc/isolate/ruby-1.8/gems/i18n-0.4.2/lib/i18n/backend/active_record.rb
depengine-0.0.25 etc/isolate/ruby-1.8/gems/i18n-0.4.2/lib/i18n/backend/active_record.rb
depengine-0.0.24 etc/isolate/ruby-1.8/gems/i18n-0.4.2/lib/i18n/backend/active_record.rb
depengine-0.0.23 etc/isolate/ruby-1.8/gems/i18n-0.4.2/lib/i18n/backend/active_record.rb
depengine-0.0.22 etc/isolate/ruby-1.8/gems/i18n-0.4.2/lib/i18n/backend/active_record.rb
depengine-0.0.21 etc/isolate/ruby-1.8/gems/i18n-0.4.2/lib/i18n/backend/active_record.rb
depengine-0.0.20 etc/isolate/ruby-1.8/gems/i18n-0.4.2/lib/i18n/backend/active_record.rb
depengine-0.0.19 etc/isolate/ruby-1.8/gems/i18n-0.4.2/lib/i18n/backend/active_record.rb
depengine-0.0.18 etc/isolate/ruby-1.8/gems/i18n-0.4.2/lib/i18n/backend/active_record.rb
depengine-0.0.17 etc/isolate/ruby-1.8/gems/i18n-0.4.2/lib/i18n/backend/active_record.rb
depengine-0.0.16 etc/isolate/ruby-1.8/gems/i18n-0.4.2/lib/i18n/backend/active_record.rb
depengine-0.0.15 etc/isolate/ruby-1.8/gems/i18n-0.4.2/lib/i18n/backend/active_record.rb
depengine-0.0.14 etc/isolate/ruby-1.8/gems/i18n-0.4.2/lib/i18n/backend/active_record.rb
depengine-0.0.13 etc/isolate/ruby-1.8/gems/i18n-0.4.2/lib/i18n/backend/active_record.rb
depengine-0.0.12 etc/isolate/ruby-1.8/gems/i18n-0.4.2/lib/i18n/backend/active_record.rb