On Tuesday, 12 February 2013 at 19:38:56 UTC, qznc wrote:
On Tuesday, 12 February 2013 at 16:38:54 UTC, FG wrote:
On 2013-02-12 15:21, Steven Schveighoffer wrote:
string pluralize(string x, int count)
{
  return count > 1 ? x ~ "s" : x;
}

You mean: return count != 1 ? x ~ "s" : x;
For special cases there could be a third optional argument:

   string pluralize(int count, string sng, string pl="") {
       return count == 1 ? sng : pl.length ? pl : sng ~ "s";
   }
   writeln(pluralize(2, "cat"));
   writeln(pluralize(2, "radius", "radii"));


That's for simple English-only pluralization, before we jump into translations.

Do not go down that rabbit hole! Just skim over the following links to get an idea how complicated this becomes.

https://developer.mozilla.org/en/docs/Localization_and_Plurals
http://doc.qt.digia.com/qq/qq19-plurals.html

I am not sure, if any internationalization stuff should be in std. No matter how you do it, it will be too simple for some and too over-engineered for others.

There will always be multiple solutions on different levels of power and applications have to choose, which suits them best.

My advice: If you find your self wanting that, try to rephrase first.

For example, instead of "there are %d cat(s)" output "number of cats: %d". It will be much easier to internationalize.


Agreed. It's such an enormous problem, even just in English. You're much better off creating a special tool that satisfies your own needs for the specific set of words that you need pluralisin.

Reply via email to