@@ -304,14 +304,124 @@ Translations in Templates
304304-------------------------
305305
306306Most of the time, translation occurs in templates. Symfony provides native
307- support for both Twig and PHP templates:
307+ support for both Twig and PHP templates.
308308
309- .. code-block :: html+twig
309+ Using Twig Tags
310+ ~~~~~~~~~~~~~~~
310311
311- <h1>{% trans %}Symfony is great!{% endtrans %}</h1>
312+ Symfony provides specialized Twig tags (``trans `` and ``transchoice ``) to
313+ help with message translation of *static blocks of text *:
314+
315+ .. code-block :: twig
316+
317+ {% trans %}Hello %name%{% endtrans %}
318+
319+ {% transchoice count %}
320+ {0} There are no apples|{1} There is one apple|]1,Inf[ There are %count% apples
321+ {% endtranschoice %}
322+
323+ The ``transchoice `` tag automatically gets the ``%count% `` variable from
324+ the current context and passes it to the translator. This mechanism only
325+ works when you use a placeholder following the ``%var% `` pattern.
326+
327+ .. deprecated :: 4.2
328+
329+ The ``transchoice `` tag is deprecated since Symfony 4.2 and will be
330+ removed in 5.0. Use the :doc: `ICU MessageFormat </translation/message_format >` with
331+ the ``trans `` tag instead.
332+
333+ .. caution ::
334+
335+ The ``%var% `` notation of placeholders is required when translating in
336+ Twig templates using the tag.
337+
338+ .. tip ::
339+
340+ If you need to use the percent character (``% ``) in a string, escape it by
341+ doubling it: ``{% trans %}Percent: %percent%%%{% endtrans %} ``
342+
343+ You can also specify the message domain and pass some additional variables:
344+
345+ .. code-block :: twig
346+
347+ {% trans with {'%name%': 'Fabien'} from 'app' %}Hello %name%{% endtrans %}
348+
349+ {% trans with {'%name%': 'Fabien'} from 'app' into 'fr' %}Hello %name%{% endtrans %}
350+
351+ {% transchoice count with {'%name%': 'Fabien'} from 'app' %}
352+ {0} %name%, there are no apples|{1} %name%, there is one apple|]1,Inf[ %name%, there are %count% apples
353+ {% endtranschoice %}
354+
355+ .. _translation-filters :
356+
357+ Using Twig Filters
358+ ~~~~~~~~~~~~~~~~~~
359+
360+ The ``trans `` and ``transchoice `` filters can be used to translate *variable
361+ texts * and complex expressions:
362+
363+ .. code-block :: twig
364+
365+ {{ message|trans }}
366+
367+ {{ message|transchoice(5) }}
368+
369+ {{ message|trans({'%name%': 'Fabien'}, 'app') }}
370+
371+ {{ message|transchoice(5, {'%name%': 'Fabien'}, 'app') }}
372+
373+ .. deprecated :: 4.2
374+
375+ The ``transchoice `` filter is deprecated since Symfony 4.2 and will be
376+ removed in 5.0. Use the :doc: `ICU MessageFormat </translation/message_format >` with
377+ the ``trans `` filter instead.
378+
379+ .. tip ::
380+
381+ Using the translation tags or filters have the same effect, but with
382+ one subtle difference: automatic output escaping is only applied to
383+ translations using a filter. In other words, if you need to be sure
384+ that your translated message is *not * output escaped, you must apply
385+ the ``raw `` filter after the translation filter:
386+
387+ .. code-block :: html+twig
388+
389+ {# text translated between tags is never escaped #}
390+ {% trans %}
391+ <h3>foo</h3>
392+ {% endtrans %}
393+
394+ {% set message = '<h3>foo</h3>' %}
395+
396+ {# strings and variables translated via a filter are escaped by default #}
397+ {{ message|trans|raw }}
398+ {{ '<h3>bar</h3>'|trans|raw }}
399+
400+ .. tip ::
401+
402+ You can set the translation domain for an entire Twig template with a single tag:
403+
404+ .. code-block :: twig
405+
406+ {% trans_default_domain 'app' %}
407+
408+ Note that this only influences the current template, not any "included"
409+ template (in order to avoid side effects).
410+
411+ PHP Templates
412+ ~~~~~~~~~~~~~
413+
414+ The translator service is accessible in PHP templates through the
415+ ``translator `` helper::
416+
417+ <?= $view['translator']->trans('Symfony is great') ?>
418+
419+ <?= $view['translator']->transChoice(
420+ '{0} There are no apples|{1} There is one apple|]1,Inf[ There are %count% apples',
421+ 10,
422+ ['%count%' => 10]
423+ ) ?>
312424
313- Read :doc: `/translation/templates ` for more information about the Twig tags and
314- filters for translation.
315425
316426Forcing the Translator Locale
317427-----------------------------
@@ -600,7 +710,6 @@ Learn more
600710 :maxdepth: 1
601711
602712 translation/message_format
603- translation/templates
604713 translation/locale
605714 translation/debug
606715 translation/lint
0 commit comments