Module: FastGettext::Translation

Extended by:
Translation
Included in:
Translation
Defined in:
lib/fast_gettext/translation.rb

Overview

this module should be included Responsibility:

- direct translation queries to the current repository
- handle untranslated values
- understand / enforce namespaces
- decide which plural form is used

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klas) ⇒ Object

make it usable in class definition, e.g. class Y

include FastGettext::Translation
@@x = _('y')

end



16
17
18
# File 'lib/fast_gettext/translation.rb', line 16

def self.included(klas)  #:nodoc:
  klas.extend self
end

Instance Method Details

#_(key, &block) ⇒ Object



20
21
22
# File 'lib/fast_gettext/translation.rb', line 20

def _(key, &block)
  FastGettext.cached_find(key) or (block ? block.call : key)
end

#n_(*keys, &block) ⇒ Object

translate pluralized some languages have up to 4 plural forms… n_(singular, plural, plural form 2, …, count) n_(‘apple’,‘apples’,3)



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fast_gettext/translation.rb', line 28

def n_(*keys, &block)
  count = keys.pop
  translations = FastGettext.cached_plural_find(*keys)

  selected = FastGettext.pluralisation_rule.call(count)
  selected = (selected ? 1 : 0) unless selected.is_a? Numeric #convert booleans to numbers

  result = translations[selected]
  if result
    result
  elsif keys[selected]
    _(keys[selected])
  else
    block ? block.call : keys.last
  end
end

#N_(translate) ⇒ Object

tell gettext: this string need translation (will be found during parsing)



53
54
55
# File 'lib/fast_gettext/translation.rb', line 53

def N_(translate)
  translate
end

#Nn_(*keys) ⇒ Object

tell gettext: this string need translation (will be found during parsing)



58
59
60
# File 'lib/fast_gettext/translation.rb', line 58

def Nn_(*keys)
  keys
end

#ns_(*args, &block) ⇒ Object



62
63
64
65
66
# File 'lib/fast_gettext/translation.rb', line 62

def ns_(*args, &block)
  translation = n_(*args, &block)
  # block is called once again to compare result
  block && translation == block.call ? translation : translation.split(NAMESPACE_SEPARATOR).last
end

#s_(key, separator = nil, &block) ⇒ Object

translate, but discard namespace if nothing was found Car|Tire -> Tire if no translation could be found



47
48
49
50
# File 'lib/fast_gettext/translation.rb', line 47

def s_(key, separator=nil, &block)
  translation = FastGettext.cached_find(key) and return translation
  block ? block.call : key.split(separator||NAMESPACE_SEPARATOR).last
end