forked from joshmh/globalize2
/
pluralizing.rb
39 lines (32 loc) · 1.11 KB
/
pluralizing.rb
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
require 'i18n/backend/simple'
module Globalize
module Backend
class Pluralizing < I18n::Backend::Simple
def pluralize(locale, entry, count)
return entry unless entry.is_a?(Hash) and count
key = pluralizer(locale).call(count)
# use other as fallback for zero if zero entry is not available
key = :other if key == :zero && !entry.has_key?(:zero)
raise I18n::InvalidPluralizationData.new(entry, count) unless entry.has_key?(key)
translation entry[key], :plural_key => key
end
def add_pluralizer(locale, pluralizer)
pluralizers[locale.to_sym] = pluralizer
end
def pluralizer(locale)
pluralizers[locale.to_sym] || default_pluralizer
end
protected
def default_pluralizer
pluralizers[:en]
end
def pluralizers
@pluralizers ||= { :en => lambda{|n| n == 1 ? :one : ( n == 0 ? :zero : :other) } }
end
# Overwrite this method to return something other than a String
def translation(string, attributes)
string
end
end
end
end