module I18n::Backend::Mongoid::Implementation

Attributes

model[RW]

Public Class Methods

new(model) click to toggle source
# File lib/i18n_backend_mongoid/i18n/backend/mongoid.rb, line 10
def initialize(model)
  @model = model
  init_translations
end

Public Instance Methods

available_locales() click to toggle source

Get available locales from the translations hash

# File lib/i18n_backend_mongoid/i18n/backend/mongoid.rb, line 31
def available_locales
  init_translations unless initialized?
  translations.inject([]) do |locales, (locale, data)|
    locales << locale unless (data.keys - [:i18n]).empty?
    locales
  end
end
initialized?() click to toggle source
# File lib/i18n_backend_mongoid/i18n/backend/mongoid.rb, line 15
def initialized?
  @initialized ||= false
end
reload!() click to toggle source

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

# File lib/i18n_backend_mongoid/i18n/backend/mongoid.rb, line 40
def reload!
  @initialized = false
  @translations = nil
  init_translations
end
store_translations(locale, data, options = {}) 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_mongoid/i18n/backend/mongoid.rb, line 23
def store_translations(locale, data, options = {})
  locale = locale.to_sym
  translations[locale] ||= {}
  data = data.deep_symbolize_keys
  translations[locale].deep_merge!(data)
end

Protected Instance Methods

init_translations() click to toggle source
# File lib/i18n_backend_mongoid/i18n/backend/mongoid.rb, line 48
def init_translations
  load_translations
  @initialized = true
end
load_translations() click to toggle source
# File lib/i18n_backend_mongoid/i18n/backend/mongoid.rb, line 53
def load_translations
  @translations = {}
  @model.all.each do |record|
    I18n.available_locales.each do |loc|
      next unless record.value_translations.key?(loc)
      k = I18n.normalize_keys(loc, record.key, [])
      k.shift
      store_translations(loc, k.reverse.inject(record.value_translations[loc]) { |a, n| { n => a } })
    end
  end
end
lookup(locale, key, scope = [], options = {}) click to toggle source

Looks up a translation from the translations hash. Returns nil if eiher 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_mongoid/i18n/backend/mongoid.rb, line 74
def lookup(locale, key, scope = [], options = {})
  init_translations unless initialized?
  keys = I18n.normalize_keys(locale, key, scope, options[:separator])

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

    result
  end
end
translations() click to toggle source
# File lib/i18n_backend_mongoid/i18n/backend/mongoid.rb, line 65
def translations
  @translations ||= {}
end