phpgettextphp-gettext

php ngettext multiple variables


Copying the example from the manual, ngettext works like this:

ngettext("%d window", "%d windows", 1); //'1 window';
ngettext("%d window", "%d windows", 2); //'2 windows';

But what if I want to use 2 variables, for example %d windows %d doors? Is there a standard way to implement this, so that the correct string (4 combinations) appears based on the 2 variables?


Solution

  • ngettext() does not support multiple variables. You have to make sure to write sentences that can be properly translated block by block (if possible multiple sentences to avoid issues with language-dependent ordering).

    At least one reason why this is not supported is that the number of requisite fallback sentences (first parameters of ngettext()) would grow exponentially with the number of variables (i.e. you would need 4 such sentences for 2 variables: singular-singular, singular-plural, plural-singular and plural-plural, then 8 for 3 variables, etc.).

    You will find an answer to a similar question at https://stackoverflow.com/a/1893929/4457767.