Pluralization: the missing part of .Net

Pluralization: the missing part of .Net

Plural forms are a lot more complex than we think, there is no generic rules to manage plurals, each languages have their specific rules. A lazy way is to ignore them:

3 item(s) in your cart

As a designer, I hate that, it’s laziness.

So a lot of applications write a code similar to this one:

if(numberOfItem == 1)
   return SINGULAR;
else
   return PLURAL;

It’s better but still wrong, except if you only support english, did you know that this rule is incorrect in French for example?

Contrary to English, the number 0 is singular as well as all numbers between 0 and 2 (excluded) so the correct code for french is:

if(number<2)
   return SINGULAR;
else
   return PLURAL;

Let take an other language, Hindi for instance. In Hindi, 0.3 is singular (plural in english, singular in french) whereas 1.8 is plural (plural in english, singular in french)

One more time, rules are different, here is the algorithm for Hindi:

if(number >= 0 && number <= 1)
   return SINGULAR;
else
   return PLURAL;

More than 2 forms

So rules are different between languages, but not all languages are binary, for some languages there is more than 2 plural forms, Czech has a third form for example:


if(number >= 2 && number <= 4)
   return FEW;
else if(number ==1)
   return SINGULAR;
else
   return PLURAL;

But there is more complicated, let’s take a look to Polish:

if ((n % 10).IsBetween(2, 4) && !(n % 100).IsBetween(12, 14))
{
   return FEW;
}
if (n != 1 && (n % 10).IsBetween(0, 1) ||
(n % 10).IsBetween(5, 9) ||
(n % 100).IsBetween(12, 14))
{
   return MANY;
}
if (n == 1)
{
   return ONE;
}
return OTHER;

4 different forms and some very complex rules, a real challenge but wait, there is more complicated.

Decimal numbers

We need also to manage decimal numbers. For a lot of readers, the number 98.41 and 98.45 will have the same plural form, but not for everyone, some languages use the last number of the fractional part to make a choice, 98.41 can be singular for some languages (because ending with 1) contrary to 98.45 (because ending with a 5).

Moreover, some languages make a distinction between 1 (singular) and and 1.0 (plural)…

The rules

Unicode released a very interesting page to understand the differences between all languages, it was one of my main sources to create this library:

http://www.unicode.org/cldr/charts/25/supplemental/language_plural_rules.html

So how it works with .Net?

This is the issue, contrary to other platforms (ios, android. etc..), .Net doesn’t manage pluralization out-of-the-box and no third-party libraries are available.

Solution

I created a new library named PluralNet, managing almost all existing languages (including regional languages), very easy to use and compatible with RESW files as well as RESX files.

More information here: http://www.rudyhuyn.com/blog/2016/09/28/pluralnet

Comments are closed.