class EacRubyUtils::Locales::ModuleI18nTranslate

def ancestor_i18n_translate(ancestor)

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

def ancestors_i18n_translate

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

def exists?

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

def i18n_key

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

def i18n_options

def i18n_options
  values.except(TRANSLATE_LOCALE_KEY)
end

def i18n_translate

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

def locale

def locale
  values[TRANSLATE_LOCALE_KEY]
end

def module_entry_prefix

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

def on_locale(&block)

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

def on_present_locale(&block)

def on_present_locale(&block)
  old_locale = ::I18n.locale
  begin
    ::I18n.locale = locale
    block.call
  ensure
    ::I18n.locale = old_locale
  end
end

def result

def result
  on_locale do
    ancestors_i18n_translate || i18n_translate
  end
end