module I18n::Backend::Simple::Implementation

Public Instance Methods

available_locales() click to toggle source

Get available locales from the translations hash

# File lib/i18n/backend/simple.rb, line 46
def available_locales
  init_translations unless initialized?
  translations.inject([]) do |locales, (locale, data)|
    locales << locale unless data.size <= 1 && (data.empty? || data.has_key?(:i18n))
    locales
  end
end
initialized?() click to toggle source
# File lib/i18n/backend/simple.rb, line 24
def initialized?
  @initialized ||= false
end
reload!() click to toggle source

Clean up translations hash and set initialized to false on reload!

Calls superclass method I18n::Backend::Base#reload!
# File lib/i18n/backend/simple.rb, line 55
def reload!
  @initialized = false
  @translations = nil
  super
end
store_translations(locale, data, options = EMPTY_HASH) click to toggle source

Stores translations for the given locale in memory. This uses a deep merge for the translations hash, so existing translations will be overwritten by new ones only at the deepest level of the hash.

# File lib/i18n/backend/simple.rb, line 32
def store_translations(locale, data, options = EMPTY_HASH)
  if I18n.enforce_available_locales &&
    I18n.available_locales_initialized? &&
    !I18n.available_locales.include?(locale.to_sym) &&
    !I18n.available_locales.include?(locale.to_s)
    return data
  end
  locale = locale.to_sym
  translations[locale] ||= {}
  data = data.deep_symbolize_keys
  translations[locale].deep_merge!(data)
end
translations(do_init: false) click to toggle source
# File lib/i18n/backend/simple.rb, line 61
def translations(do_init: false)
  # To avoid returning empty translations,
  # call `init_translations`
  init_translations if do_init && !initialized?

  @translations ||= {}
end

Protected Instance Methods

init_translations() click to toggle source
# File lib/i18n/backend/simple.rb, line 71
def init_translations
  load_translations
  @initialized = true
end
lookup(locale, key, scope = [], options = EMPTY_HASH) click to toggle source

Looks up a translation from the translations hash. Returns nil if either key is nil, or locale, scope or key do not exist as a key in the nested translations hash. Splits keys or scopes containing dots into multiple keys, i.e. currency.format is regarded the same as %w(currency format).

# File lib/i18n/backend/simple.rb, line 81
def lookup(locale, key, scope = [], options = EMPTY_HASH)
  init_translations unless initialized?
  keys = I18n.normalize_keys(locale, key, scope, options[:separator])

  keys.inject(translations) do |result, _key|
    _key = _key.to_sym
    return nil unless result.is_a?(Hash) && result.has_key?(_key)
    result = result[_key]
    result = resolve(locale, _key, result, options.merge(:scope => nil)) if result.is_a?(Symbol)
    result
  end
end