Project

pluralize

0.0
No commit activity in last 3 years
No release in over 3 years
Better pluralization for non-english languages
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

Pluralize - Better pluralization for non-english languages¶ ↑

Languages vary in how they handle plurals of nouns or unit expressions (“hours”, “meters”, and so on). Some languages have two forms, like English; some languages have only a single form; and some languages have multiple forms.

Read more at: www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html

Installation¶ ↑

The gem is hosted on gemcutter, so if you haven’t already, add it as a gem source:

sudo gem sources -a http://gemcutter.org/

Then install the Formtastic gem:

sudo gem install pluralize

Configuration¶ ↑

ActiveSupport::Inflector.inflections do |inflect|
  inflect.proc :pl, lambda{|count, singular, inflections|
    if count == 1
      singular
    elsif (2..4).include?(count%10) && !((12..14).to_a + (22..24).to_a).include?(count%100)
      inflections[:few]
    else
      inflections[:other]
    end
  }

  inflect.plural "chleb", :few => "chleby", :other => "chlebów", :proc => :pl
  inflect.plural "szklanka", :few => "szklanki", :other => "szklanek" # use first defined proc
  inflect.plural "one potatoe",
    :two => "two potatoes",
    :three => "three potatoes",
    :four => "four",
    :five => "five potatoes",
    :six => "six potatoes",
    :seven => "seven potatoes",
    :more => "more!",
    :proc => lambda{|count, singular, inflections|
      case count%8
      when 1 then singular
      when 2 then inflections[:two]
      when 3 then inflections[:three]
      when 4 then inflections[:four]
      when 5 then inflections[:five]
      when 6 then inflections[:six]
      when 7 then inflections[:seven]
      else inflections[:more]
      end
  }
end

Usage¶ ↑

ActiveSupport::Inflector.pluralize("chleb", 1)        # => chleb
ActiveSupport::Inflector.pluralize("chleb", 2)        # => chleby
"chleb".pluralize                                     # => chleby
ActiveSupport::Inflector.pluralize("chleb", 5)        # => chlebów
ActiveSupport::Inflector.pluralize("one potatoe", 7)  # => seven potatoes
ActiveSupport::Inflector.pluralize("one potatoe", 10) # => more!

# for other words it behaves like before
ActiveSupport::Inflector.pluralize("word")            # => words
ActiveSupport::Inflector.pluralize("octopus")         # => octopi
"person".pluralize                                    # => people

#in your views

<%= pluralize(3, "chleb") %>                          # => chleby

Author¶ ↑

Marcin Ciunelis marcin.ciunelis@gmail.com

Copyright © 2009 G-Forces Polska, released under the MIT license