c73a009a505032d86d51a84574c27ed7597e2cb9
[packages/precise/mcollective.git] / lib / mcollective / vendor / i18n / lib / i18n / backend / pluralization.rb
1 # I18n Pluralization are useful when you want your application to
2 # customize pluralization rules.
3 #
4 # To enable locale specific pluralizations you can simply include the
5 # Pluralization module to the Simple backend - or whatever other backend you
6 # are using.
7 #
8 #   I18n::Backend::Simple.include(I18n::Backend::Pluralization)
9 #
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.
12 module I18n
13   module Backend
14     module Pluralization
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')
19       #
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.
24       #
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
31
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)
36           entry[key]
37         else
38           super
39         end
40       end
41
42       protected
43
44         def pluralizers
45           @pluralizers ||= {}
46         end
47
48         def pluralizer(locale)
49           pluralizers[locale] ||= I18n.t(:'i18n.plural.rule', :locale => locale, :resolve => false)
50         end
51     end
52   end
53 end