I think we’ve all run into the issue before where an app says that there are “1 minutes remaining”, or something along those lines. I think this observation is a testament to the fact that many programmers ignore this issue.
In my projects, I’ve typically done something along these lines to account for pluralizing nouns:
$Count = count($Items);
$Noun = 'minute';
if ($Count != 1)
{
$Noun .= 's';
}
echo sprintf('There are %u %s remaining.', $Count, $Noun);
I have a couple issues with this approach:
- It places the onus on the programmer to do this pluralization check every single time a string needs to be generated, so code can never be reused.
- It needlessly bloats the application code and inhibits readability.
- It is not generic. The example worked because “minutes” is the plural of “minute”. What about “sheep”, “ox”, or “fungus”?
Does anyone have any ideas for a generic, modular approach to solve this problem? I’m ok with there being more than just one answer.
It is common to implement a function that accepts a number and all singular and plural adjectives.
So sample invocation could be
If you’ll decide to translate your application into many languages this link will help to pluralize correctly: http://translate.sourceforge.net/wiki/l10n/pluralforms
ps: the propisal I made will work fine with any languages in case if you pass additional 3rd parameter
languagethat will specify which pluralization formula to use.