Created
October 7, 2010 14:30
-
-
Save nicolasblanco/615174 to your computer and use it in GitHub Desktop.
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
# Pluralize helper for French language. | |
# | |
# Because French pluralization has special rules : | |
# Example : | |
# pluralize(0, "degré") # => "0 degré" | |
# pluralize(1, "degré") # => "1 degré" | |
# pluralize(2, "degré") # => "2 degrés" | |
# pluralize(0.5, "degré") # => "0.5 degré" | |
# pluralize(1.9, "degré") # => "1.9 degré" | |
# pluralize(-1.9, "degré") # => "-1.9 degré" | |
# pluralize(-2, "degré") # => "-2 degrés" | |
# | |
# Options : | |
# :without_count : if you don't want to output the count (only the word), | |
# :plural : to use a custom plural. | |
# | |
def pluralize(count, singular, options = {}) | |
"".tap do |result| | |
result << "#{count || 0} " unless options[:without_count] | |
result << (count.to_i > -2 && count.to_i < 2 ? singular : (options[:plural] || singular.pluralize)).to_s | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The Rails i18n system provides a lot of features to translate strings, interpolation, date/time localizing... And of course, know how to pluralize words.
This helper does not implement "singular" or "pluralize" which are already implemented in Rails i18n.
This is just an additionnal language RULE.
In English we say "0 + plural", in other language it may be different. In french we say "0 + singular". This helper is to override this rule for the French language.