Rails gem: Localized pluralization with i18n

Paul Engel di 27 jul 10

As you are working on an internalization Rails application, you are maybe using Globalize2 along with I18n to translate models (e.g. categories). The end-user is probably able to manage the translations in different locales such as Dutch, German, French, Spanish and so forth.

>> I18n.locale
=> :en
>> (c = Category.first).name
=> "birthday"
>> I18n.locale = :nl
=> :nl
>> c.name
=> "verjaardag"
>> I18n.locale = :fr
=> :fr
>> c.name
=> "anniversaire"

Sometimes you want to display the translation in pluralized form. For instance:

>> I18n.locale
=> :en
>> "Found " + c.name.pluralize
=> "Found birthdays"

A solution to accomplish this is to translate both singular and plural form:

>> I18n.locale
=> :nl
>> I18n.t("found").capitalize + " " + c.name_pluralized
=> "Gevonden verjaardagen"

There are some cons though:

  • you have to translate in singular AND plural form
  • you have to use two columns (name and name_pluralized)

That’s some sort of redundancy as we want to keep things DRY: only translate in singular form and pluralize the translation. Fortunately, there is a Rails gem that does just that:

Rich-pluralization is a E9s module which pluralizes words with inflection rules of the current locale. You can compare it with the ActiveSupport::Inflector, except that the inflections do not influence the Rails pluralization (which is used for methods as tableize and classify).

Looking at our previous example, this is what the implementation can look like:

>> I18n.locale
=> :nl
>> I18n.t("found").capitalize + " " + c.name.pl
=> "Gevonden verjaardagen"

And of course, you can pluralize static strings:

>> I18n.locale
=> :nl
>> "Fiets".pl
=> "Fietsen"
>> "MUSEUM".pl
=> "MUSEA"

Please note: the letter casing stays preserved(!)

All in all, using Rich-pluralization provides you to only translate in singular form and call .pl for the pluralized translation. Now isn’t that a DRY implementation?

At the moment, Rich-pluralization is shipped with Dutch inflections. It covers 86% of the 54977 words provided by INL. This will increase as the developers are working hard on improving the inflections. Also, you can also add inflections for other locales.

Please visit the Github project page if you are interested for more information.

Gepost in hor |  0 reacties

Plaats je reactie





Welkom op Holland On Rails

Het startpunt voor Ruby On Rails in Nederland. Vind de laatste technieken, meningen en nieuwtjes.

Recente Jobs

Gezocht: Ruby On Rails ontwikkelaar (junior of senior)

Eet, drink en droom jij over Ruby On Rails? Wil jij het liefste dag en nacht bezig zijn met jehobby; super coole webapplicaties ontwikkelen in Ruby On Rails?

Dan willen wij jou graag een podium bieden om je Ruby skills te vertonen aan onze nationale en internationale klanten!

@ Internetbureau Holder, Obdam

Bekijk alle jobs »»

Gereedschapskist

Onmisbare tools voor
iedere developer!
Ruby On Rails
Framework voor de web 2.0 developer. Eindelijk vooruitgang!
TextMate
Editor for true pro's
Typ, tab, top :-)
Nee, niet voor Win.
Made On A Mac
En nou is het over met die saaie grijze Windows bak van je!

Auteurs op deze site

Chris Obdam

'Less is more' evangelist, past dit ook dagelijks toe op zijn tandenborstel.

Chiel Wester

Snelheidswonder op Ruby wielen. Leuk om mee te pair-programmen ;-) Recommend Me
Src-120-attending

Stephan Kaag

Het eerste Rails coreteam- member uit Nederland? Rails evangelist van het eerste uur.

Paul Engel

Én Rails programmeren én interfaces designen? Je zou hem superman kunnen noemen..

Dax Huiberts

Official Zip-Programmer, skinny code is helemaal zijn ding. Haalt meer code weg dan dat er bij komt.

Freek Monteban

Het nieuwste telg uit het Holland on Rails nest! Hij doet niets anders meer!

Johan Vermeulen

De stylesheet-koning uit de kop van Noord-Holland!