]> git.openstreetmap.org Git - rails.git/blob - vendor/plugins/globalize2/lib/globalize/backend/pluralizing.rb
Merged 16488:16743 from trunk.
[rails.git] / vendor / plugins / globalize2 / lib / globalize / backend / pluralizing.rb
1 require 'i18n/backend/simple'
2
3 module Globalize
4   module Backend
5     class Pluralizing < I18n::Backend::Simple
6       def pluralize(locale, entry, count)
7         return entry unless entry.is_a?(Hash) and count
8         key = :zero if count == 0 && entry.has_key?(:zero)
9         key ||= pluralizer(locale).call(count, entry)
10         raise InvalidPluralizationData.new(entry, count) unless entry.has_key?(key)
11         translation entry[key], :plural_key => key
12       end
13
14       def add_pluralizer(locale, pluralizer)
15         pluralizers[locale.to_sym] = pluralizer
16       end
17
18       def pluralizer(locale)
19         pluralizers[locale.to_sym] || default_pluralizer
20       end
21
22       protected
23         def default_pluralizer
24           pluralizers[:en]
25         end
26
27         def pluralizers
28           @pluralizers ||= {
29             :en => lambda { |count, entry|
30               case count
31                 when 1 then entry.has_key?(:one) ? :one : :other
32                 else :other
33               end
34             },
35             :sl => lambda { |count, entry|
36               case count % 100
37                 when 1 then entry.has_key?(:one) ? :one : :other
38                 when 2 then entry.has_key?(:two) ? :two : :other
39                 when 3,4 then entry.has_key?(:few) ? :few : :other
40                 else :other
41               end
42             }
43           }
44         end
45
46         # Overwrite this method to return something other than a String
47         def translation(string, attributes)
48           string
49         end
50     end
51   end
52 end