"When digging through some code that was on the refactor list, I came accross some validation logic that checks if the user selected enough options on the form," writes Chris Osgood, "if enough options weren't selected, you'd get an error message along that said something like 'at least 3 options are required'."
"It took a little bit of coding to get that validate message. The '3' was obviously dynamic, and if there was only one missing selection, then 'are' was replaced with 'is'. As for the word 'option'...it was is PLURALIZED!"
/// <summary> /// Changes a singular string to plural e.g. "Monkey" => "Monkeys". /// </summary> /// <param name="text">The text to PLURALIZE!</param> /// <returns>A plural string.</returns> public static string Pluralize(string text) { if (text.Length == 0) { return text; } string result = text; if (result.Equals("sheep", StringComparison.OrdinalIgnoreCase)) { return "sheep"; } else if (result.Equals("leaf", StringComparison.OrdinalIgnoreCase)) { return "leaves"; } else if (result.Equals("thief", StringComparison.OrdinalIgnoreCase)) { return "thieves"; } else if (result.Equals("potato", StringComparison.InvariantCultureIgnoreCase)) { return "potatoes"; } else if (result.EndsWith("y", StringComparison.InvariantCultureIgnoreCase) && !result.EndsWith("ey", StringComparison.InvariantCultureIgnoreCase)) { // Don't pluralize "by" if (result.Length > 2 && !result.EndsWith(" by", StringComparison.InvariantCultureIgnoreCase)) { result = result.Truncate(result.Length - 1) + "ies"; } } else if (result.EndsWith("us", StringComparison.InvariantCultureIgnoreCase)) { // http://en.wikipedia.org/wiki/Plural_form_of_words_ending_in_-us result += "es"; } else if (result.EndsWith("x", StringComparison.InvariantCultureIgnoreCase)) { result += "es"; } else if (!result.EndsWith("s", StringComparison.InvariantCultureIgnoreCase)) { result += "s"; } return result; }
Chris continued, "naturally, this was the only usage of this otherwise highly useful function. We could have easily utilized this function to pluralize things like foots, tomatos, boies, wifes, and etc.!"