@@ -253,16 +253,39 @@ all the forms as a string separated by a pipe (``|``)::
253
253
To translate pluralized messages, use the
254
254
:method: `Symfony\\ Component\\ Translation\\ Translator::transChoice ` method::
255
255
256
+ // the %count% placeholder is assigned to the second argument...
256
257
$translator->transChoice(
257
258
'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.',
258
265
10,
259
- array('%count%' => 10)
266
+ // no need to include %count% here; Symfony does that for you
267
+ array('%name%' => $user->getName())
260
268
);
261
269
262
270
The second argument (``10 `` in this example) is the *number * of objects being
263
271
described and is used to determine which translation to use and also to populate
264
272
the ``%count% `` placeholder.
265
273
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
+
266
289
Based on the given number, the translator chooses the right plural form.
267
290
In English, most words have a singular form when there is exactly one object
268
291
and a plural form for all other numbers (0, 2, 3...). So, if ``count `` is
@@ -366,11 +389,25 @@ use for translation::
366
389
$translator->transChoice(
367
390
'{0} There are no apples|{1} There is one apple|]1,Inf[ There are %count% apples',
368
391
10,
369
- array('%count%' => 10 ),
392
+ array(),
370
393
'messages',
371
394
'fr_FR'
372
395
);
373
396
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
+
374
411
Retrieving the Message Catalogue
375
412
--------------------------------
376
413
0 commit comments