{"id":260,"date":"2005-04-22T07:31:25","date_gmt":"2005-04-22T15:31:25","guid":{"rendered":"http:\/\/boulter.com\/blog\/2005\/04\/22\/5-ways-to-pluralize\/"},"modified":"2005-12-18T12:47:24","modified_gmt":"2005-12-18T20:47:24","slug":"5-ways-to-pluralize","status":"publish","type":"post","link":"http:\/\/boulter.com\/blog\/2005\/04\/22\/5-ways-to-pluralize\/","title":{"rendered":"5 way(s) to pluralize"},"content":{"rendered":"<p>Programmers are lazy or maybe they just can&#8217;t count. More and more I see sites out there with dumb things like &#8220;1 page(s)&#8221; or worse just &#8220;1 pages&#8221;.  I could find lots of Yahoo pages with this kind of stuff, but that&#8217;s beside the point &#8211; I&#8217;m providing help for lazy programmers who can&#8217;t count. <\/p>\n<p>The rule is simple. Words are plural for every value <strong>except<\/strong> 1.  But now you don&#8217;t even have to remember that. Just cut and paste one of the functions below. <\/p>\n<p>To use any of them, call them with the count of things and then the singular form of the word and the plural form. For the truly lazy, if you can pluralize your word by just appending an &#8220;s&#8221; to the end, the last argument is optional. But don&#8217;t do this with words like &#8220;quantity&#8221;, duh.<\/p>\n<p><strong><br \/>\nPHP Example:<\/strong><\/p>\n<pre>\r\nI know 5 lazy < ?php echo pluralize(5, 'programmer', 'programmers'); ?>.\r\n\r\nI know 5 lazy < ?php echo pluralize(5, 'programmer'); ?>.\r\n\r\nTheir < ?php echo pluralize(1, 'mommy', 'mommies') ?> didn't teach them to count.\r\n<\/pre>\n<p><strong>Spits out:<br \/>\n<\/strong><\/p>\n<pre>\r\nI know 5 lazy programmers.\r\n\r\nI know 5 lazy programmers.\r\n\r\nTheir mommy didn't teach them to count.\r\n<\/pre>\n<p>Here&#8217;s the code to cut &#8216;n paste:<\/p>\n<p><strong>PHP<\/strong><\/p>\n<pre>\r\nfunction pluralize($count, $singular, $plural = false)\r\n{\r\n   if (!$plural) $plural = $singular . 's';\r\n\r\n  return ($count == 1 ? $singular : $plural) ;\r\n}\r\n<\/pre>\n<p><i>I&#8217;m surprised this isn&#8217;t already Yet Another PHP Function<\/i><\/p>\n<p><strong>Perl<\/strong><\/p>\n<pre>\r\nsub pluralize()\r\n{\r\n   my ($count, $singular, $plural) = @_;\r\n\r\n   $plural = $singular . 's' if (!$plural) ;\r\n\r\n  return $singular if ($count == 1);\r\n  return $plural; \r\n}\r\n<\/pre>\n<p><em>I&#8217;m sure someone will come along and rewrite this in one illegible line. Of course there&#8217;s already <a HREF=\"http:\/\/search.cpan.org\/~kvail\/Text-Pluralize-1.0\/lib\/Text\/Pluralize.pm\">a CPAN module for this<\/a> if you&#8217;re afraid of writing any code yourself.<\/em><\/p>\n<p><strong>Java<\/strong><\/p>\n<pre>\r\npublic static String pluralize(int count, String singular)\r\n{\r\n   return pluralize(count, singular, singular.concat('s'));\r\n}\r\n\r\npublic static String pluralize(int count, String singular, String plural)\r\n{\r\n  return (count == 1 ? singular : plural) \r\n}\r\n<\/pre>\n<p><em>Boy I wish Java had optional arguments.<\/em><\/p>\n<p><strong>JavaScript<\/strong><\/p>\n<pre>\r\nString.prototype.pluralize = function(count, plural)\r\n{\r\n  if (plural == null)\r\n    plural = this + 's';\r\n\r\n  return (count == 1 ? this : plural) \r\n}\r\n<\/pre>\n<p><em>OO JavaScript is cool<\/em><\/p>\n<p>Please contribute your favorite oddball language. It&#8217;s too early in the morning for me to write in C and I get hives anytime I look at Visual Basic.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Programmers are lazy or maybe they just can&#8217;t count. More and more I see sites out there with dumb things like &#8220;1 page(s)&#8221; or worse just &#8220;1 pages&#8221;. I could find lots of Yahoo pages with this kind of stuff, but that&#8217;s beside the point &#8211; I&#8217;m providing help for lazy programmers who can&#8217;t count. &#8230;<\/p>\n<p><a href=\"http:\/\/boulter.com\/blog\/2005\/04\/22\/5-ways-to-pluralize\/\" class=\"more-link\">Continue reading &lsquo;5 way(s) to pluralize&rsquo; &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-260","post","type-post","status-publish","format-standard","hentry","category-general"],"_links":{"self":[{"href":"http:\/\/boulter.com\/blog\/wp-json\/wp\/v2\/posts\/260","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/boulter.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/boulter.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/boulter.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/boulter.com\/blog\/wp-json\/wp\/v2\/comments?post=260"}],"version-history":[{"count":0,"href":"http:\/\/boulter.com\/blog\/wp-json\/wp\/v2\/posts\/260\/revisions"}],"wp:attachment":[{"href":"http:\/\/boulter.com\/blog\/wp-json\/wp\/v2\/media?parent=260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/boulter.com\/blog\/wp-json\/wp\/v2\/categories?post=260"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/boulter.com\/blog\/wp-json\/wp\/v2\/tags?post=260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}