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) ⇒ Object



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

def _(key)
  FastGettext.cached_find(key) or key
end

#n_(*keys) ⇒ 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
# File 'lib/fast_gettext/translation.rb', line 28

def n_(*keys)
  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
  translations[selected] || keys[selected] || keys.last
end

#N_(translate) ⇒ Object

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



44
45
46
# File 'lib/fast_gettext/translation.rb', line 44

def N_(translate)
  translate
end

#Nn_(*keys) ⇒ Object

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



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

def Nn_(*keys)
  keys
end

#s_(key, seperator = nil) ⇒ Object

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



38
39
40
41
# File 'lib/fast_gettext/translation.rb', line 38

def s_(key,seperator=nil)
  translation = FastGettext.cached_find(key) and return translation
  key.split(seperator||NAMESPACE_SEPERATOR).last
end