Home > pluralize

pluralize

Pluralize is a project mainly written in Ruby, based on the MIT license.

Better pluralization for non-english languages

== 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: http://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 [email protected]

Copyright (c) 2009 G-Forces Polska, released under the MIT license