Skip to content

Commit 65d2f99

Browse files
committed
minor #7205 Simplified the use of transChoice() (javiereguiluz)
This PR was squashed before being merged into the 3.2 branch (closes #7205). Discussion ---------- Simplified the use of transChoice() This fixes #7172. I'm not happy with the result, so I'm open to comments to improve this. Thanks! Commits ------- 1e4e9a7 Simplified the use of transChoice()
2 parents 34246ef + 1e4e9a7 commit 65d2f99

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

components/translation/usage.rst

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,16 +253,39 @@ all the forms as a string separated by a pipe (``|``)::
253253
To translate pluralized messages, use the
254254
:method:`Symfony\\Component\\Translation\\Translator::transChoice` method::
255255

256+
// the %count% placeholder is assigned to the second argument...
256257
$translator->transChoice(
257258
'There is one apple|There are %count% apples',
259+
10
260+
);
261+
262+
// ...but you can define more placeholders if needed
263+
$translator->transChoice(
264+
'Hurry up %name%! There is one apple left.|There are %count% apples left.',
258265
10,
259-
array('%count%' => 10)
266+
// no need to include %count% here; Symfony does that for you
267+
array('%name%' => $user->getName())
260268
);
261269

262270
The second argument (``10`` in this example) is the *number* of objects being
263271
described and is used to determine which translation to use and also to populate
264272
the ``%count%`` placeholder.
265273

274+
.. versionadded:: 3.2
275+
276+
Before Symfony 3.2, the placeholder used to select the plural (``%count%``
277+
in this example) must be included in the third optional argument of the
278+
``transChoice()`` method::
279+
280+
$translator->transChoice(
281+
'There is one apple|There are %count% apples',
282+
10,
283+
array('%count%' => 10)
284+
);
285+
286+
Starting from Symfony 3.2, when the only placeholder is ``%count%``, you
287+
don't have to pass this third argument.
288+
266289
Based on the given number, the translator chooses the right plural form.
267290
In English, most words have a singular form when there is exactly one object
268291
and a plural form for all other numbers (0, 2, 3...). So, if ``count`` is
@@ -366,11 +389,25 @@ use for translation::
366389
$translator->transChoice(
367390
'{0} There are no apples|{1} There is one apple|]1,Inf[ There are %count% apples',
368391
10,
369-
array('%count%' => 10),
392+
array(),
370393
'messages',
371394
'fr_FR'
372395
);
373396

397+
.. note::
398+
399+
Starting from Symfony 3.2, the third argument of ``transChoice()`` is
400+
optional when the only placeholder in use is ``%count%``. In previous
401+
Symfony versions you needed to define it always::
402+
403+
$translator->transChoice(
404+
'{0} There are no apples|{1} There is one apple|]1,Inf[ There are %count% apples',
405+
10,
406+
array('%count%' => 10),
407+
'messages',
408+
'fr_FR'
409+
);
410+
374411
Retrieving the Message Catalogue
375412
--------------------------------
376413

0 commit comments

Comments
 (0)