Does Java have a straightforward method for pluralizing a word? If not, I am wondering why it is not available when Rails has one.
Any specific reasons?
jtahlborn has a good point with regards to language specificity. That said, this is what I wrote for use in our app. It's not comprehensive, but it's easy enough to add exceptions as you hit them.
This is C#, but should be close enough or you to adapt:
public string GetPlural(string singular)
{
string CONSONANTS = "bcdfghjklmnpqrstvwxz";
switch (singular)
{
case "Person":
return "People";
case "Trash":
return "Trash";
case "Life":
return "Lives";
case "Man":
return "Men";
case "Woman":
return "Women";
case "Child":
return "Children";
case "Foot":
return "Feet";
case "Tooth":
return "Teeth";
case "Dozen":
return "Dozen";
case "Hundred":
return "Hundred";
case "Thousand":
return "Thousand";
case "Million":
return "Million";
case "Datum":
return "Data";
case "Criterion":
return "Criteria";
case "Analysis":
return "Analyses";
case "Fungus":
return "Fungi";
case "Index":
return "Indices";
case "Matrix":
return "Matrices";
case "Settings":
return "Settings";
case "UserSettings":
return "UserSettings";
default:
// Handle ending with "o" (if preceeded by a consonant, end with -es, otherwise -s: Potatoes and Radios)
if (singular.EndsWith("o") && CONSONANTS.Contains(singular[singular.Length - 2].ToString()))
{
return singular + "es";
}
// Handle ending with "y" (if preceeded by a consonant, end with -ies, otherwise -s: Companies and Trays)
if (singular.EndsWith("y") && CONSONANTS.Contains(singular[singular.Length - 2].ToString()))
{
return singular.Substring(0, singular.Length - 1) + "ies";
}
// Ends with a whistling sound: boxes, buzzes, churches, passes
if (singular.EndsWith("s") || singular.EndsWith("sh") || singular.EndsWith("ch") || singular.EndsWith("x") || singular.EndsWith("z"))
{
return singular + "es";
}
return singular + "s";
}
}
An additional usage note... As you can probably see, it expects whatever word you're passing in to have the first letter capitalized (doesn't matter if it's not in the exceptions list). There are a myriad of a ways of handling this differently. It's simply what met our particular needs.