module Decidim::TermCustomizer::I18nBackend::Implementation

Public Instance Methods

available_locales() click to toggle source

Get available locales from the translations hash

# File lib/decidim/term_customizer/i18n_backend.rb, line 17
def available_locales
  Translation.available_locales
rescue ::ActiveRecord::StatementInvalid
  []
end
initialized?() click to toggle source
# File lib/decidim/term_customizer/i18n_backend.rb, line 23
def initialized?
  !@translations.nil?
end
reload!() click to toggle source

Clean up translations hash on reload!

Calls superclass method
# File lib/decidim/term_customizer/i18n_backend.rb, line 28
def reload!
  @translations = nil
  super
end
translations() click to toggle source
# File lib/decidim/term_customizer/i18n_backend.rb, line 33
def translations
  return @translations if @translations
  return {} unless TermCustomizer.loader

  @translations = TermCustomizer.loader.translations_hash
end

Protected Instance Methods

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/decidim/term_customizer/i18n_backend.rb, line 47
def lookup(locale, key, scope = [], options = EMPTY_HASH)
  keys = I18n.normalize_keys(locale, key, scope, options[:separator])

  keys.inject(translations) do |result, inner_key|
    return nil unless result.is_a?(Hash)

    unless result.has_key?(inner_key)
      inner_key = inner_key.to_s.to_sym
      return nil unless result.has_key?(inner_key)
    end
    result = result[inner_key]
    result = resolve(locale, inner_key, result, options.merge(scope: nil)) if result.is_a?(Symbol)
    result
  end
end