1 # I18n Pluralization are useful when you want your application to
2 # customize pluralization rules.
4 # To enable locale specific pluralizations you can simply include the
5 # Pluralization module to the Simple backend - or whatever other backend you
8 # I18n::Backend::Simple.include(I18n::Backend::Pluralization)
10 # You also need to make sure to provide pluralization algorithms to the
11 # backend, i.e. include them to your I18n.load_path accordingly.
15 # Overwrites the Base backend translate method so that it will check the
16 # translation meta data space (:i18n) for a locale specific pluralization
17 # rule and use it to pluralize the given entry. I.e. the library expects
18 # pluralization rules to be stored at I18n.t(:'i18n.plural.rule')
20 # Pluralization rules are expected to respond to #call(count) and
21 # return a pluralization key. Valid keys depend on the translation data
22 # hash (entry) but it is generally recommended to follow CLDR's style,
23 # i.e., return one of the keys :zero, :one, :few, :many, :other.
25 # The :zero key is always picked directly when count equals 0 AND the
26 # translation data has the key :zero. This way translators are free to
27 # either pick a special :zero translation even for languages where the
28 # pluralizer does not return a :zero key.
29 def pluralize(locale, entry, count)
30 return entry unless entry.is_a?(Hash) and count
32 pluralizer = pluralizer(locale)
33 if pluralizer.respond_to?(:call)
34 key = count == 0 && entry.has_key?(:zero) ? :zero : pluralizer.call(count)
35 raise InvalidPluralizationData.new(entry, count) unless entry.has_key?(key)
48 def pluralizer(locale)
49 pluralizers[locale] ||= I18n.t(:'i18n.plural.rule', :locale => locale, :resolve => false)