-
-
Save stiff/7964 to your computer and use it in GitHub Desktop.
lambda replaced with an instance of a new class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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