module FastGettext::Translation

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

Public Instance Methods

N_(translate) click to toggle source

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

# File lib/fast_gettext/translation.rb, line 60
def N_(translate)
  translate
end
Nn_(*keys) click to toggle source

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

# File lib/fast_gettext/translation.rb, line 65
def Nn_(*keys)
  keys
end
_(key) { |: key)| ... } click to toggle source
# File lib/fast_gettext/translation.rb, line 14
def _(key)
  FastGettext.cached_find(key) || (block_given? ? yield : key)
end
n_(*keys, count) { || ... } click to toggle source

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

# 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
np_(context, plural_one, *args, separator: nil) { || ... } click to toggle source

translate pluralized with context

# 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) { || ... } click to toggle source

translate pluralized with separator

# 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) { |: key| ... } click to toggle source

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

# 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) { |: split(separator || NAMESPACE_SEPARATOR).last| ... } click to toggle source

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

# 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