Module: FastGettext::Translation

Included in:
FastGettext, TranslationAliased, TranslationMultidomain
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

Instance Method Summary collapse

Instance Method Details

#_(key) ⇒ Object



14
15
16
# File 'lib/fast_gettext/translation.rb', line 14

def _(key)
  FastGettext.cached_find(key) || (block_given? ? yield : key)
end

#n_(*keys, count) ⇒ Object

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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fast_gettext/translation.rb', line 22

def n_(*keys, count)
  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

  # If we have a translation return it
  result = translations[selected]
  return result if result

  # If we have a block always use it in place of a translation
  return yield if block_given?

  # Fall back to the best fit translated key if it's there
  _(keys[selected] || keys.last)
end

#N_(translate) ⇒ Object

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



60
61
62
# File 'lib/fast_gettext/translation.rb', line 60

def N_(translate)
  translate
end

#Nn_(*keys) ⇒ Object

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



65
66
67
# File 'lib/fast_gettext/translation.rb', line 65

def Nn_(*keys)
  keys
end

#np_(context, plural_one, *args, separator: nil) ⇒ Object

translate pluralized with context



80
81
82
83
84
85
86
87
88
# File 'lib/fast_gettext/translation.rb', line 80

def np_(context, plural_one, *args, separator: nil)
  nargs = ["#{context}#{separator || CONTEXT_SEPARATOR}#{plural_one}"] + args
  translation = n_(*nargs, &NIL_BLOCK)
  return translation if translation

  return yield if block_given?

  n_(plural_one, *args)
end

#ns_(*args) ⇒ Object

translate pluralized with separator



70
71
72
73
74
75
76
77
# File 'lib/fast_gettext/translation.rb', line 70

def ns_(*args)
  translation = n_(*args, &NIL_BLOCK)
  return translation if translation

  return yield if block_given?

  n_(*args).split(NAMESPACE_SEPARATOR).last
end

#p_(namespace, key, separator = nil) ⇒ Object

translate with namespace ‘Car’, ‘Tire’ -> Tire if no translation could be found p_(‘Car’, ‘Tire’) == s_(‘Car|Tire’)



41
42
43
44
45
46
47
48
# File 'lib/fast_gettext/translation.rb', line 41

def p_(namespace, key, separator = nil)
  msgid = "#{namespace}#{separator || CONTEXT_SEPARATOR}#{key}"

  translation = FastGettext.cached_find(msgid)
  return translation if translation

  block_given? ? yield : key
end

#s_(key, separator = nil) ⇒ Object

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



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

def s_(key, separator = nil)
  translation = FastGettext.cached_find(key)
  return translation if translation

  block_given? ? yield : key.split(separator || NAMESPACE_SEPARATOR).last
end