module I18n::Backend::ActiveRecord::Implementation

Public Instance Methods

available_locales() click to toggle source
# File lib/i18n/backend/active_record.rb, line 14
def available_locales
  begin
    Translation.available_locales
  rescue ::ActiveRecord::StatementInvalid
    []
  end
end
store_translations(locale, data, options = {}) click to toggle source
# File lib/i18n/backend/active_record.rb, line 22
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 Instance Methods

expand_keys(key) click to toggle source

For a key :'foo.bar.baz' return ['foo', 'foo.bar', 'foo.bar.baz']

# File lib/i18n/backend/active_record.rb, line 51
def expand_keys(key)
  key.to_s.split(FLATTEN_SEPARATOR).inject([]) do |keys, key|
    keys << [keys.last, key].compact.join(FLATTEN_SEPARATOR)
  end
end
lookup(locale, key, scope = [], options = {}) click to toggle source
# File lib/i18n/backend/active_record.rb, line 32
def lookup(locale, key, scope = [], options = {})
  key = normalize_flat_keys(locale, key, scope, options[:separator])
  result = Translation.locale(locale).lookup(key)

  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