<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>Rails gem: Localized pluralization with i18n</title>
    <link>http://www.hollandonrails.nl/articles/521-Rails-gem-Localized-pluralization-with-i18n</link>
    <description>As you are working on an internalization Rails application, you are maybe using "Globalize2":http://github.com/joshmh/globalize2 along with "I18n":http://github.com/svenfuchs/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.

&lt;code&gt;
&gt;&gt; I18n.locale
=&gt; :en
&gt;&gt; (c = Category.first).name
=&gt; "birthday"
&gt;&gt; I18n.locale = :nl
=&gt; :nl
&gt;&gt; c.name
=&gt; "verjaardag"
&gt;&gt; I18n.locale = :fr
=&gt; :fr
&gt;&gt; c.name
=&gt; "anniversaire"
&lt;/code&gt;

Sometimes you want to display the translation in pluralized form. For instance:

&lt;code&gt;
&gt;&gt; I18n.locale
=&gt; :en
&gt;&gt; "Found " + c.name.pluralize
=&gt; "Found birthdays"
&lt;/code&gt;

A solution to accomplish this is to translate both singular and plural form:

&lt;code&gt;
&gt;&gt; I18n.locale
=&gt; :nl
&gt;&gt; I18n.t("found").capitalize + " " + c.name_pluralized
=&gt; "Gevonden verjaardagen"
&lt;/code&gt;

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":http://github.com/archan937/rich_pluralization is a "E9s":http://github.com/archan937/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@":http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#M000718 and "@classify@":http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#M000719). 

Looking at our previous example, this is what the implementation can look like:

&lt;code&gt;
&gt;&gt; I18n.locale
=&gt; :nl
&gt;&gt; I18n.t("found").capitalize + " " + c.name.pl
=&gt; "Gevonden verjaardagen"
&lt;/code&gt;

And of course, you can pluralize static strings:

&lt;code&gt;
&gt;&gt; I18n.locale
=&gt; :nl
&gt;&gt; "Fiets".pl
=&gt; "Fietsen"
&gt;&gt; "MUSEUM".pl
=&gt; "MUSEA"
&lt;/code&gt;

&lt;small&gt;Please note: the letter casing stays preserved(!)&lt;/small&gt;

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 (Institution of Dutch Lexicology)":http://www.inl.nl. 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":http://github.com/archan937/rich_pluralization if you are interested for more information.</description>
  </channel>
</rss>

