Java and how to deal with the singular plural form

The singular plural variation is an annoyance that you’ve probably had to deal with many times.

On a lot of scenarios you can just get rid of the problem with something like this:

So if you were populating a dashboard with users you would feed the method with singular - user and plural - users. But recently I was browsing StackOverflow and stumbled across this answer, that reflects the intrinsics of different languages, in this particular case, Czech.

Java 11 has a handy construct called ChoiceFormat that allows you to deal with this scenario in a very elegant way:

plurals, by the camera of Geran de Klerk

There is another project that’s a worthy mention, Evo Inflector. Based on the work of Damian Conway – “An Algorithmic Approach to English Pluralization” – does wonders for most cases of the English language. But it’s an external dependency, more than often will be overkill to what you actually need and only has guaranteed coverage to ~70% of the words.

Link to Oracle’s ChoiceFormat documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *