1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
module FastGettext
# 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
module Translation
extend self
#make it usable in class definition, e.g.
# class Y
# include FastGettext::Translation
# @@x = _('y')
# end
def self.included(klas) #:nodoc:
klas.extend self
end
def _(key, &block)
FastGettext.cached_find(key) or (block ? block.call : key)
end
#translate pluralized
# some languages have up to 4 plural forms...
# n_(singular, plural, plural form 2, ..., count)
# n_('apple','apples',3)
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
#translate, but discard namespace if nothing was found
# Car|Tire -> Tire if no translation could be found
def s_(key, separator=nil, &block)
translation = FastGettext.cached_find(key) and return translation
block ? block.call : key.split(separator||NAMESPACE_SEPARATOR).last
end
#tell gettext: this string need translation (will be found during parsing)
def N_(translate)
translate
end
#tell gettext: this string need translation (will be found during parsing)
def Nn_(*keys)
keys
end
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
end
# this module should be included for multi-domain support
module TranslationMultidomain
extend self
#make it usable in class definition, e.g.
# class Y
# include FastGettext::TranslationMultidomain
# @@x = d_('domain', 'y')
# end
def self.included(klas) #:nodoc:
klas.extend self
end
# helper block for changing domains
def _in_domain domain
old_domain = FastGettext.text_domain
FastGettext.text_domain = domain
yield if block_given?
ensure
FastGettext.text_domain = old_domain
end
# gettext functions to translate in the context of given domain
def d_(domain, key, &block)
_in_domain domain do
FastGettext::Translation._(key, &block)
end
end
def dn_(domain, *keys, &block)
_in_domain domain do
FastGettext::Translation.n_(*keys, &block)
end
end
def ds_(domain, key, separator=nil, &block)
_in_domain domain do
FastGettext::Translation.s_(key, separator, &block)
end
end
def dns_(domain, *keys, &block)
_in_domain domain do
FastGettext::Translation.ns_(*keys, &block)
end
end
# gettext functions to translate in the context of any domain
# (note: if mutiple domains contains key, random translation is returned)
def D_(key)
FastGettext.translation_repositories.each_key do |domain|
result = FastGettext::TranslationMultidomain.d_(domain, key) {nil}
return result unless result.nil?
end
key
end
def Dn_(*keys)
FastGettext.translation_repositories.each_key do |domain|
result = FastGettext::TranslationMultidomain.dn_(domain, *keys) {nil}
return result unless result.nil?
end
keys[-3].split(keys[-2]||NAMESPACE_SEPARATOR).last
end
def Ds_(key, separator=nil)
FastGettext.translation_repositories.each_key do |domain|
result = FastGettext::TranslationMultidomain.ds_(domain, key, separator) {nil}
return result unless result.nil?
end
key.split(separator||NAMESPACE_SEPARATOR).last
end
def Dns_(*keys)
FastGettext.translation_repositories.each_key do |domain|
result = FastGettext::TranslationMultidomain.dns_(domain, *keys) {nil}
return result unless result.nil?
end
keys[-2].split(NAMESPACE_SEPARATOR).last
end
end
end
|