In PHP, I use Kuwamoto's class to pluralize nouns in my strings. I didn't find something as good as this script in javascript except for some plugins. So, it would be great to have a javascript function based on Kuwamoto's class.
http://kuwamoto.org/2007/12/17/improved-pluralizing-in-php-actionscript-and-ror/
Rules for foreign plurals of Latin-derived wordsFor words ending in -um, -um is changed to -a: addendum to addenda. For words ending in -us, -us is changed to -i: fungus to fungi. For words ending in -ex, -ex is changed to -ices: index to indices.
noun, plural keys.
Simple version (ES6):
const maybePluralize = (count, noun, suffix = 's') => `${count} ${noun}${count !== 1 ? suffix : ''}`;
Usage:
maybePluralize(0, 'turtle'); // 0 turtles maybePluralize(1, 'turtle'); // 1 turtle maybePluralize(2, 'turtle'); // 2 turtles maybePluralize(3, 'fox', 'es'); // 3 foxes
This obviously doesn't support all english edge-cases, but it's suitable for most purposes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With