Sha256: b24dda69cf9a9135b5c8a2e8b26fa964a472fbfd75b0df5c96cba104dce6cec9

Contents?: true

Size: 1.48 KB

Versions: 22

Compression:

Stored size: 1.48 KB

Contents

class I18n::Backend::ActiveRecord
  include Singleton
  include I18n::Backend::Base
  include I18n::Backend::Flatten
  include I18n::Backend::Memoize
  include I18n::Backend::Cache
  def available_locales
    begin
      ::Apiv1::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|
      ::Apiv1::Translation.locale(locale).lookup(expand_keys(key)).delete_all
      ::Apiv1::Translation.create(:locale => locale.to_s, :key => key.to_s, :value => value)
    end
  end

  private

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

    if result.empty?
      nil
    elsif result.first.key == key
      result.first.value
    else
      chop_range = (key.size + I18n::Backend::Flatten::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(I18n::Backend::Flatten::FLATTEN_SEPARATOR).inject([]) do |keys, key|
      keys << [keys.last, key].compact.join(I18n::Backend::Flatten::FLATTEN_SEPARATOR)
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
cloudrider-0.3.23 generica/app/models/i18n/backend/active_record.rb
cloudrider-0.3.22 generica/app/models/i18n/backend/active_record.rb
cloudrider-0.3.21 generica/app/models/i18n/backend/active_record.rb
cloudrider-0.3.20 generica/app/models/i18n/backend/active_record.rb
cloudrider-0.3.0 generica/app/models/i18n/backend/active_record.rb
cloudrider-0.2.18 generica/app/models/i18n/backend/active_record.rb
cloudrider-0.2.15 generica/app/models/i18n/backend/active_record.rb
cloudrider-0.2.14 generica/app/models/i18n/backend/active_record.rb
cloudrider-0.2.12 generica/app/models/i18n/backend/active_record.rb
cloudrider-0.2.11 generica/app/models/i18n/backend/active_record.rb
cloudrider-0.2.10 generica/app/models/i18n/backend/active_record.rb
cloudrider-0.2.9 generica/app/models/i18n/backend/active_record.rb
cloudrider-0.2.8 generica/app/models/i18n/backend/active_record.rb
cloudrider-0.2.7 generica/app/models/i18n/backend/active_record.rb
cloudrider-0.2.6 generica/app/models/i18n/backend/active_record.rb
cloudrider-0.2.5 generica/app/models/i18n/backend/active_record.rb
cloudrider-0.2.4 generica/app/models/i18n/backend/active_record.rb
cloudrider-0.2.3 generica/app/models/i18n/backend/active_record.rb
cloudrider-0.2.2 generica/app/models/i18n/backend/active_record.rb
cloudrider-0.2.1 generica/app/models/i18n/backend/active_record.rb