Add russian pluralization logic, based on description in the russion
[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         key = :other unless entry.has_key?(key)
11         raise InvalidPluralizationData.new(entry, count) unless entry.has_key?(key)
12         translation entry[key], :plural_key => key
13       end
14
15       def add_pluralizer(locale, pluralizer)
16         pluralizers[locale.to_sym] = pluralizer
17       end
18
19       def pluralizer(locale)
20         pluralizers[locale.to_sym] || default_pluralizer
21       end
22
23       protected
24         def default_pluralizer
25           pluralizers[:en]
26         end
27
28         def pluralizers
29           @pluralizers ||= {
30             :en => lambda { |count, entry|
31               case count
32                 when 1 then :one
33                 else :other
34               end
35             },
36             :ru => lambda { |count, entry|
37               case count % 100
38                 when 11,12,13,14 then :many
39                 else case count % 10
40                        when 1 then :one
41                        when 2,3,4 then :few
42                        when 5,6,7,8,9,0 then :many
43                        else :other
44                      end
45               end
46             },
47             :sl => lambda { |count, entry|
48               case count % 100
49                 when 1 then :one
50                 when 2 then :two
51                 when 3,4 then :few
52                 else :other
53               end
54             }
55           }
56         end
57
58         # Overwrite this method to return something other than a String
59         def translation(string, attributes)
60           string
61         end
62     end
63   end
64 end