Skip to content

Commit

Permalink
changed pluralizer to also handle zero values from lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Oey committed May 28, 2009
1 parent e7eb1bf commit 6c3df7b
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/globalize/backend/pluralizing.rb
Expand Up @@ -3,11 +3,13 @@
module Globalize
module Backend
class Pluralizing < I18n::Backend::Simple

def pluralize(locale, entry, count)
return entry unless entry.is_a?(Hash) and count
key = :zero if count == 0 && entry.has_key?(:zero)
key ||= pluralizer(locale).call(count)
raise InvalidPluralizationData.new(entry, count) unless entry.has_key?(key)
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

Expand All @@ -18,14 +20,14 @@ def add_pluralizer(locale, pluralizer)
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 : :other } }
@pluralizers ||= { :en => lambda{|n| n == 1 ? :one : ( n == 0 ? :zero : :other) } }
end

# Overwrite this method to return something other than a String
Expand Down

0 comments on commit 6c3df7b

Please sign in to comment.