Class: EacRubyUtils::Locales::ModuleI18nTranslate

Inherits:
Object
  • Object
show all
Defined in:
lib/eac_ruby_utils/locales/module_i18n_translate.rb

Constant Summary collapse

TRANSLATE_LOCALE_KEY =
:__locale

Instance Method Summary collapse

Instance Method Details

#ancestor_i18n_translate(ancestor) ⇒ Object



14
15
16
17
# File 'lib/eac_ruby_utils/locales/module_i18n_translate.rb', line 14

def ancestor_i18n_translate(ancestor)
  t = self.class.new(ancestor, entry_suffix, values)
  t.exists? ? t.i18n_translate : nil
end

#ancestors_i18n_translateObject



19
20
21
# File 'lib/eac_ruby_utils/locales/module_i18n_translate.rb', line 19

def ancestors_i18n_translate
  the_module.ancestors.lazy.map { |v| ancestor_i18n_translate(v) }.find(&:present?)
end

#exists?Boolean

Returns:



23
24
25
# File 'lib/eac_ruby_utils/locales/module_i18n_translate.rb', line 23

def exists?
  ::I18n.exists?(i18n_key)
end

#i18n_keyObject



27
28
29
# File 'lib/eac_ruby_utils/locales/module_i18n_translate.rb', line 27

def i18n_key
  "#{module_entry_prefix}.#{entry_suffix}"
end

#i18n_optionsObject



31
32
33
# File 'lib/eac_ruby_utils/locales/module_i18n_translate.rb', line 31

def i18n_options
  values.except(TRANSLATE_LOCALE_KEY)
end

#i18n_translateObject



35
36
37
# File 'lib/eac_ruby_utils/locales/module_i18n_translate.rb', line 35

def i18n_translate
  ::I18n.translate(i18n_key, i18n_options)
end

#localeObject



39
40
41
# File 'lib/eac_ruby_utils/locales/module_i18n_translate.rb', line 39

def locale
  values[TRANSLATE_LOCALE_KEY]
end

#module_entry_prefixObject



43
44
45
# File 'lib/eac_ruby_utils/locales/module_i18n_translate.rb', line 43

def module_entry_prefix
  the_module.name.underscore.gsub('/', '.')
end

#on_locale(&block) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/eac_ruby_utils/locales/module_i18n_translate.rb', line 47

def on_locale(&block)
  if locale.present?
    on_present_locale(&block)
  else
    block.call
  end
end

#resultObject



55
56
57
58
59
# File 'lib/eac_ruby_utils/locales/module_i18n_translate.rb', line 55

def result
  on_locale do
    ancestors_i18n_translate || i18n_translate
  end
end