Skip to content

Instantly share code, notes, and snippets.

@stiff
Forked from yaroslav/simple.rb
Created August 29, 2008 12:54
Show Gist options
  • Save stiff/7964 to your computer and use it in GitHub Desktop.
Save stiff/7964 to your computer and use it in GitHub Desktop.
lambda replaced with an instance of a new class
module Pluralizations
# Uses English pluralization rules -- it will pick the first translation if count is not equal to 1
# and the second translation if it is equal to 1.
class OneOther
def get_symbol(n)
n == 1 ? :one : :other
end
end
end
# Picks a pluralization rule specified in translation tables for a language or
# uses default pluralization rules.
#
# This is how pluralization rules are defined in translation tables, English
# language for example:
#
# store_translations :'en-US', {
# :pluralize => Pluralizations::OneOther.new
# }
#
# Rule must return a symbol to use with pluralization, it must be one of:
# :zero, :one, :two, :few, :many, :other
def pluralize(locale, entry, count)
return entry unless entry.is_a?(Hash) and count
key = :zero if count == 0 && entry.has_key?(:zero)
key ||= locale.pluralize.get_symbol(count)
raise InvalidPluralizationData.new(entry, count) unless entry.has_key?(key)
entry[key]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment