Module: I18n::Backend::Chain::Implementation

Included in:
I18n::Backend::Chain
Defined in:
lib/exvo_globalize/backend/chain.rb

Instance Method Summary collapse

Instance Method Details

#available_app_translationsObject

Return a Hash only with Application translations



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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_translationsObject

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



8
9
10
11
# 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) ⇒ Object

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



14
15
16
17
18
19
20
21
22
# 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_storeObject

I18n.backend.globalize_store



84
85
86
# File 'lib/exvo_globalize/backend/chain.rb', line 84

def globalize_store
  backends.detect { |backend| backend.is_a?(I18n::Backend::GlobalizeStore) }
end

#simpleObject

I18n.backend.simple



79
80
81
# 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) ⇒ Object

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



70
71
72
73
74
# 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) ⇒ Object

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



56
57
58
59
60
61
62
63
64
65
66
# 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) ⇒ Object

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



45
46
47
48
49
50
51
52
53
# 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