Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/translatable/string.rb

Instance Method Summary collapse

Instance Method Details

#translate(*args) ⇒ Object Also known as: t

Translate a string.

Pass args which in turn are passed to String’s % method, used for placeholders which should not be translated such as peoples names, emails, etc.

When the language is english (default), this process is simply bypassed, returning the original string. Otherwise a lookup on the translations hash is performed, searching for a match, otherwise again the original string is returned.

There are times when you may want to override strings even within the english language simply for flexibility, to do this set Translateable::BypassEnglishTranslations to false.

Examples:

'Hello'.t                                      # => Hello
'Hello %s'.t 'TJ'                              # => Hello TJ
'Hello %s, welcome to %s'.t 'TJ', 'Magic Land' # => Hello TJ, welcome to Magic Land
'Welcome to %2$s %1$s'.t 'TJ', 'Magic Land'    # => Welcome to Magic Land TJ


29
30
31
32
33
34
35
36
37
# File 'lib/translatable/string.rb', line 29

def translate *args
  t = Translatable
  if t.language == :en and not t.english_translations_allowed?
    self % args
  else
    s = t.translations[t.language][self] rescue self
    s % args
  end
end