module I18n::Backend::ActiveRecord::Implementation

Public Instance Methods

available_locales() click to toggle source
# File lib/i18n/backend/active_record.rb, line 32
def available_locales
  Translation.available_locales
rescue ::ActiveRecord::StatementInvalid
  []
end
init_translations() click to toggle source
# File lib/i18n/backend/active_record.rb, line 66
def init_translations
  @translations = Translation.to_h
end
initialized?() click to toggle source
# File lib/i18n/backend/active_record.rb, line 62
def initialized?
  !@translations.nil?
end
reload!() click to toggle source
# File lib/i18n/backend/active_record.rb, line 56
def reload!
  @translations = nil

  self
end
store_translations(locale, data, options = {}) click to toggle source
# File lib/i18n/backend/active_record.rb, line 38
def store_translations(locale, data, options = {})
  escape = options.fetch(:escape, true)

  flatten_translations(locale, data, escape, false).each do |key, value|
    translation = Translation.locale(locale).lookup(expand_keys(key))

    if self.class.config.cleanup_with_destroy
      translation.destroy_all
    else
      translation.delete_all
    end

    Translation.create(locale: locale.to_s, key: key.to_s, value: value)
  end

  reload! if self.class.config.cache_translations
end
translations(do_init: false) click to toggle source
# File lib/i18n/backend/active_record.rb, line 70
def translations(do_init: false)
  init_translations if do_init || !initialized?
  @translations ||= {}
end

Protected Instance Methods

build_translation_hash_by_key(lookup_key, translation) click to toggle source
# File lib/i18n/backend/active_record.rb, line 106
def build_translation_hash_by_key(lookup_key, translation)
  hash = {}

  chop_range = if lookup_key == ''
    0..-1
  else
    (lookup_key.size + FLATTEN_SEPARATOR.size)..-1
  end
  translation_nested_keys = translation.key.slice(chop_range).split(FLATTEN_SEPARATOR)
  translation_nested_keys.each.with_index.inject(hash) do |iterator, (key, index)|
    iterator[key] = translation_nested_keys[index + 1] ?  {} : translation.value
    iterator[key]
  end

  hash
end
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 124
def expand_keys(key)
  key.to_s.split(FLATTEN_SEPARATOR).inject([]) do |keys, k|
    keys << [keys.last, k].compact.join(FLATTEN_SEPARATOR)
  end
end
lookup(locale, key, scope = [], options = {}) click to toggle source
# File lib/i18n/backend/active_record.rb, line 77
def lookup(locale, key, scope = [], options = {})
  key = normalize_flat_keys(locale, key, scope, options[:separator])
  key = key[1..-1] if key.first == '.'
  key = key[0..-2] if key.last == '.'

  if self.class.config.cache_translations
    keys = ([locale] + key.split(I18n::Backend::Flatten::FLATTEN_SEPARATOR)).map(&:to_sym)

    return translations.dig(*keys)
  end

  result = if key == ''
    Translation.locale(locale).all
  else
    Translation.locale(locale).lookup(key)
  end

  if result.empty?
    nil
  elsif result.first.key == key
    result.first.value
  else
    result = result.inject({}) do |hash, translation|
      hash.deep_merge build_translation_hash_by_key(key, translation)
    end
    result.deep_symbolize_keys
  end
end