Search code examples
javapythonlocalizationpluralsingular

Is there a one-liner for dealing with singular/plural words in common languages?


All too often I see websites do things like 1 views, 1 days left, or 1 answers. To me this is just lazy as its often as easy to fix as something like:

if(views == 1)
   print views + " view"
else print views + " views"

What I want to know is if there is a one liner in a common language like java, python, php, etc., something that I can comment on sites that do this and say, its as easy as adding this to your code. Is that possible?


Solution

  • I use ternary operators when handling this on my sites. It looks like many c-based programming languages support ternary operators. It is as easy as this in php:

    <?= $views . ' view' . ($views == 1 ? '' : 's'); ?>