Module: Localite::Translate

Included in:
Localite
Defined in:
lib/localite/translate.rb

Defined Under Namespace

Classes: Missing

Instance Method Summary collapse

Instance Method Details

#translate(s, raise_mode) ⇒ Object

translate a string

returns the translated string in the current locale. If no translation is found try the base locale. If still no translation is found, return nil



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/localite/translate.rb', line 25

def translate(s, raise_mode)
  old_i18n_locale = I18n.locale
  
  [ current_locale, base ].uniq.each do |locale|
    current_scope.each(s) do |scope|
      next unless value = translate_via_i18n(locale, scope)

      log_translation s, locale, scope, value
      
      #
      # reformat: if target format is html, convert the value into text.
      return Localite::Format.send(current_format, value)
    end
  end

  src = caller[1]
  if src =~ /^([^:]+:[^:]+):/
    src = $1
  end
  logger.warn "[#{current_locale}] Could not translate #{current_scope.first(s).inspect}; from #{src}"

  record_missing current_locale, current_scope.first(s)
  return if raise_mode == :no_raise
  
  raise Missing, [current_locale, s]
ensure
  I18n.locale = old_i18n_locale
end