Take the 2-minute tour ×

There are many instances in my work projects where I need to display the size of some collection in a sentence. For example, if the collection's size is 5, it will say "5 users". If it is size of 1 or 0, it will say "1 user" or "0 user". Right now, I'm doing it with if-else statement to determine whether to print the "s" or not, which is tedious.

I'm wondering if there's an open source JSP custom tag library that allows me to accomplish this. I know I can write one myself... basically, it will have 2 parameters like this: <lib:display word="user" collection="userList" />. Depending on the collection size, it will determine whether to append an "s" or not. But then, this implementation is not going to be too robust because I also need to handle "ies" and some words don't use any of those. So, instead of creating a half-baked tool, I'm hoping there's a more robust library I could utilize right away. I'm not too worried about prefixing the word with is/are in this case.

I use Java, by the way.

Thanks much.

share|improve this question

4 Answers 4

up vote 21 down vote accepted

Take a look at inflector, a java project which lets you do Noun.pluralOf("user"), or Noun.pluralOf("user", userList.size()), and which handles a bunch of variations and unusual cases (person->people, loaf->loaves, etc.), as well as letting you define custom mapping rules when necessary.

share|improve this answer
The wealth of stuff available for Java is amazing! Thanks for this info, which could save me and others a trip into the bowels of RoR. +1. Up next: KitchenSink.jar :) –  Carl Smotricz Jul 6 '10 at 19:26
Okay... seriously, WOW! :) I thought you are just pulling my legs, and in fact, this is for real! Thank you so much, I'll indeed mess around with it soon. @Carl: Yes, we need kitchenSink.jar too. –  limc Jul 6 '10 at 20:27
+1 for the surprise! factor –  omerkudat Jul 7 '10 at 13:11
If you are unable to access the Inflector project on java.net like me, you can get it from here: mirrors.ibiblio.org/pub/mirrors/maven2/net/java/dev/inflector/… –  Brad Mar 26 '11 at 14:46
I also saw an inflector on github. Not sure if it's the same one... github.com/flextao/inflector –  matt burns Jul 8 at 13:23

Hmm, I don't quite see why you need a library for this. I would think the function to do it is trivial:

public String singlePlural(int count, String singular, String plural)
  return count==1 ? singular : plural;

Calls would look like:

singlePlural(count, "user", "users");
singlePlural(count, "baby", "babies");
singlePlural(count, "person", "people");
singlePlural(count, "cherub", "cherubim");
... etc ...

Maybe this library does a whole bunch of other things that make it useful. I suppose you could say that it supplies a dictionary of what all the plural forms are, but in any given program you don't care about the plurals of all the words in the language, just the ones you are using in this program. I guess if the word that could be singular or plural is not known at compile time, if it's something entered by the user, then I'd want a third party dictionary rather than trying to build one myself.


Suddenly it occurs to me that what you were looking for was a function for making plurals generically, embodying a set of rules like "normally just add 's', but if the word ends in 'y' change the 'y' to 'ies', if it ends in 's' change it to 'ses', ..." etc. I think in English that would be impossible for any practical purpose: there are too many special cases, like "person/people" and "child/children" etc. I think the best you could do would be to have a generic "add an 's'" rule, maybe a few other common cases, and then a long list of exceptions. Perhaps in other languages one could come up with a fairly simple rule.

So as I say, if the word is not known at compile time but comes from some user input, then yes, a third-party dictionary is highly desirable.

share|improve this answer
I'll keep this approach in mind, but in my case, the noun is something pulled programmatically from the database. –  limc Jul 6 '10 at 22:02
Ah, well, that gets you to the "if the word is not known at compile time" exception that I mentioned. So yes, in that case, having a third party dictionary is very useful. See my update. –  Jay Jul 7 '10 at 13:50
Your method won't handle multiple languages. Plural and singular variations can be a mess. One way you could do is like this: String function plural(rule, int count, Array<String> strings){ int plural = rule(count); return strings[plural]; } Not valide code but you should understand. Rule is a function passed to determine the plural index. There should be a test to return index 0 if plural doesn't exist. Anyway this method suck. You should only have to insert the "key" and the count...like in gettext. –  Loïc Faure-Lacroix Aug 16 '10 at 20:27
@Sybiam: When you say it wouldn't work, do you mean my first method, i.e. just say "here's the singular and here's the plural"? Or my second method mentioning a rule? My intent was to say that that was a bad idea. It would be wildly impractical in English because there are so many exceptions. In Latin it would be tough because there are five basic rules and then many exceptions. I suppose it would work in Esperanto. Any other languages with one consistent rule for forming plurals? My guess is that it's very few. –  Jay Aug 16 '10 at 20:59
@Sybiam: I meant to say, I think my first method would work in almost any language. In languages that have single/dual/plural as noted elsewhere on this question, you'd need three forms. If there are other languages that get more complicated, then I guess it would break down. I18n'ing adds another level of complexity. –  Jay Aug 16 '10 at 21:01

This gets complicated in languages other than English, that inflector aims to support in the future.

I am familiar with Czech where user = uživatel and:

1 uživatel
2 uživatelé
3 uživatelé
4 uživatelé
5 uživatelů


You can see why programs written with hardcoded singular+plural would get un-i18n-able.

share|improve this answer
This is why I like languages that don't care about singular or plural, like Bahasa Melayu, ex: "1 orang", "2 orang" regardless of x users. :) –  limc Jul 6 '10 at 22:00
Really? You're saying that in Czech there is a form for 1, another form for 2-4, and a different form for 5? How interesting. Why 5 rather than, say, 6, or 42? Are there different numbers for different words? I'm no philologist, but this sounds so peculiar that it's quite interesting. –  Jay Jul 7 '10 at 13:35
Really. And it is not just Czech. You might be interested in reading about "dual" as addition to "singular" and "plural" here: en.wikipedia.org/wiki/Dual_(grammatical_number) –  Roman Zenka Jul 7 '10 at 22:01
@Jay I don't know czech, but other languages have gender for words (kinda like you have actor/actress in english, only for all the words) and singular/plural forms for both genders. Also, in romanian we have cases where the suffix is different based on the role the noun plays in the sentence. While you in english might say 'to the user', we would add the suffix '-ului' to our word for user (utilizator -> utilizatorului; and this is for the male version). This makes these language less ambiguous, but also harder to learn. (verbs also have suffixes based on person/time combinations). –  Andrei Fierbinteanu Jul 21 '10 at 9:48
Ah, and we also make the articulated form using a suffix so 'the user' also has a different form (utilizator - > utilizatorul) –  Andrei Fierbinteanu Jul 21 '10 at 9:50

This functionality is built into Ruby on Rails. I don't know exactly where, but it should be easy enough to find in the source code, and then you could simply crib the code.

EDIT: Found you some code:

If I remember correctly, it's mainly a matter of appending an "s" to most words, though I believe there is a list (probably hash, err dictionary) of some common exceptions. Notable is the conversion from "person" to "people" :)

You would of course be in for a world of pain if you decided you want to internationalize this to other languages than English. Welcome to the world of highly irregular grammars, and good luck!

share|improve this answer
Thank you! Hell, if I have to internationalize this, I might as well make up my own language where a plural word always ends with a YAY... ex: 5 userYAY. :) –  limc Jul 6 '10 at 20:26

Your Answer


