@@ -337,14 +337,88 @@ Translations in Templates
337337-------------------------
338338
339339Most of the time, translation occurs in templates. Symfony provides native
340- support for both Twig and PHP templates:
340+ support for both Twig and PHP templates.
341341
342- .. code-block :: html+twig
342+ Using Twig Tags
343+ ~~~~~~~~~~~~~~~
344+
345+ Symfony provides a specialized Twig tag ``trans `` to help with message
346+ translation of *static blocks of text *:
347+
348+ .. code-block :: twig
349+
350+ {% trans %}Hello %name%{% endtrans %}
351+
352+ .. caution ::
353+
354+ The ``%var% `` notation of placeholders is required when translating in
355+ Twig templates using the tag.
356+
357+ .. tip ::
358+
359+ If you need to use the percent character (``% ``) in a string, escape it by
360+ doubling it: ``{% trans %}Percent: %percent%%%{% endtrans %} ``
361+
362+ You can also specify the message domain and pass some additional variables:
363+
364+ .. code-block :: twig
365+
366+ {% trans with {'%name%': 'Fabien'} from 'app' %}Hello %name%{% endtrans %}
367+
368+ {% trans with {'%name%': 'Fabien'} from 'app' into 'fr' %}Hello %name%{% endtrans %}
369+
370+ .. _translation-filters :
371+
372+ Using Twig Filters
373+ ~~~~~~~~~~~~~~~~~~
374+
375+ The ``trans `` filter can be used to translate *variable texts * and complex expressions:
376+
377+ .. code-block :: twig
378+
379+ {{ message|trans }}
380+
381+ {{ message|trans({'%name%': 'Fabien'}, 'app') }}
382+
383+ .. tip ::
384+
385+ Using the translation tags or filters have the same effect, but with
386+ one subtle difference: automatic output escaping is only applied to
387+ translations using a filter. In other words, if you need to be sure
388+ that your translated message is *not * output escaped, you must apply
389+ the ``raw `` filter after the translation filter:
390+
391+ .. code-block :: html+twig
392+
393+ {# text translated between tags is never escaped #}
394+ {% trans %}
395+ <h3>foo</h3>
396+ {% endtrans %}
397+
398+ {% set message = '<h3>foo</h3>' %}
399+
400+ {# strings and variables translated via a filter are escaped by default #}
401+ {{ message|trans|raw }}
402+ {{ '<h3>bar</h3>'|trans|raw }}
403+
404+ .. tip ::
405+
406+ You can set the translation domain for an entire Twig template with a single tag:
407+
408+ .. code-block :: twig
409+
410+ {% trans_default_domain 'app' %}
411+
412+ Note that this only influences the current template, not any "included"
413+ template (in order to avoid side effects).
414+
415+ PHP Templates
416+ ~~~~~~~~~~~~~
343417
344- <h1>{% trans %}Symfony is great!{% endtrans %}</h1>
418+ The translator service is accessible in PHP templates through the
419+ ``translator `` helper::
345420
346- Read :doc: `/translation/templates ` for more information about the Twig tags and
347- filters for translation.
421+ <?= $view['translator']->trans('Symfony is great') ?>
348422
349423Forcing the Translator Locale
350424-----------------------------
@@ -608,7 +682,6 @@ Learn more
608682 :maxdepth: 1
609683
610684 translation/message_format
611- translation/templates
612685 translation/locale
613686 translation/debug
614687 translation/lint
0 commit comments