module I18n::Backend::Chain::Implementation

Public Instance Methods

available_app_translations() click to toggle source

Return a Hash only with Application translations

# File lib/exvo_globalize/backend/chain.rb, line 25
def available_app_translations
  # save original load_path
  load_path = I18n.load_path

  # load only app translations (Simple I18n backend)
  I18n.load_path = Dir.glob(File.join(Rails.root, 'config/locales', '**', '*.{yml,rb}'))
  simple.reload!
  simple.send(:init_translations)
  translations = simple.available_translations

  # restore original translations
  I18n.load_path = load_path
  simple.reload!
  simple.send(:init_translations)

  # return app's translations
  translations
end
available_translations() click to toggle source

Extend the Chain backend with a custom ‘available_translations` method returning a combined Hash with translations from all chained backends

# File lib/exvo_globalize/backend/chain.rb, line 8
def available_translations
  # reverse, so that the translations from the first backend (GlobalizeStore) overwrite/overshadow others
  @available_translations ||= backends.map { |backend| backend.available_translations }.reverse.inject(&:deep_merge)
end
available_translations_scoped_by_locale_with_default(locale) click to toggle source

Returns a Hash of translations for the specified locale merged with translations for the default_locale

# File lib/exvo_globalize/backend/chain.rb, line 14
def available_translations_scoped_by_locale_with_default(locale)
  default_locale_translations = { I18n.default_locale => available_translations.fetch(I18n.default_locale) { {} } }

  if locale != I18n.default_locale
    default_locale_translations.deep_merge({ locale => available_translations.fetch(locale) { {} } })
  else
    default_locale_translations
  end
end
globalize_store() click to toggle source

I18n.backend.globalize_store

# File lib/exvo_globalize/backend/chain.rb, line 84
def globalize_store
  backends.detect { |backend| backend.is_a?(I18n::Backend::GlobalizeStore) }
end
simple() click to toggle source

I18n.backend.simple

# File lib/exvo_globalize/backend/chain.rb, line 79
def simple
  backends.detect { |backend| backend.is_a?(I18n::Backend::Simple) }
end
store_flatten_translation(*args) click to toggle source

pass-through for the ‘store_flatten_translation()` method to the GlobalizeStore so that I18n.backend.store_flatten_translation() works

# File lib/exvo_globalize/backend/chain.rb, line 70
def store_flatten_translation(*args)
  backends.each do |backend|
    return backend.send(:store_flatten_translation, *args) if backend.respond_to?(:store_flatten_translation)
  end
end
store_flatten_translations(translations_hash) click to toggle source

stores a whole Hash of flattened translations (like those: { :en => { ‘session.title’ => ‘Title’ } })

# File lib/exvo_globalize/backend/chain.rb, line 56
def store_flatten_translations(translations_hash)
  return false if translations_hash.blank?

  translations_hash.reject { |locale, translations| locale.to_sym == :default_locale }.each do |locale, translations|
    next if translations.blank?

    translations.each do |key, value|
      store_flatten_translation(locale, key, value)
    end
  end
end
store_nested_translations(translations_hash) click to toggle source

stores a whole Hash of nested translations (like those: { :en => { :session => { :title => ‘Title’ } } })

# File lib/exvo_globalize/backend/chain.rb, line 45
def store_nested_translations(translations_hash)
  return false if translations_hash.blank?

  translations_hash.reject { |locale, translations| locale.to_sym == :default_locale }.each do |locale, translations|
    next if translations.blank?

    store_translations(locale, translations)
  end
end