Skip to content

Commit 5ab6b02

Browse files
committed
Merge branch '5.2' into 5.x
* 5.2: [Translation] Move the "templates" section into the main guide [Intl] Fix typos [Session] Add missing semicolon
2 parents 5dfd10f + dc4236e commit 5ab6b02

File tree

5 files changed

+84
-98
lines changed

5 files changed

+84
-98
lines changed

_build/redirection_map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@
459459
/templating/embedding_controllers /templates#embedding-controllers
460460
/templating/inheritance /templates#template-inheritance-and-layouts
461461
/testing/doctrine /testing/database
462+
/translation/templates /translation#translation-in-templates
462463
/doctrine/lifecycle_callbacks /doctrine/events
463464
/doctrine/event_listeners_subscribers /doctrine/events
464465
/doctrine/common_extensions /doctrine

components/intl.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ to catching the exception, you can also check if a given language code is valid:
110110

111111
$isValidLanguage = Languages::exists($languageCode);
112112

113-
Or if you have a alpha3 language code you want to check::
113+
Or if you have an alpha3 language code you want to check::
114114

115115
$isValidLanguage = Languages::alpha3CodeExists($alpha3Code);
116116

@@ -197,7 +197,7 @@ to catching the exception, you can also check if a given country code is valid::
197197

198198
$isValidCountry = Countries::exists($alpha2Code);
199199

200-
Or if you have a alpha3 country code you want to check::
200+
Or if you have an alpha3 country code you want to check::
201201

202202
$isValidCountry = Countries::alpha3CodeExists($alpha3Code);
203203

session/locale_sticky_session.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ via some "Change Locale" route & controller), or create a route with the :ref:`_
109109
$container->register(LocaleSubscriber::class)
110110
->addArgument('%kernel.default_locale%')
111111
// uncomment the next line if you are not using autoconfigure
112-
// ->addTag('kernel.event_subscriber');
112+
// ->addTag('kernel.event_subscriber')
113+
;
113114
114115
That's it! Now celebrate by changing the user's locale and seeing that it's
115116
sticky throughout the request.

translation.rst

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,14 +337,88 @@ Translations in Templates
337337
-------------------------
338338

339339
Most 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

349423
Forcing 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

translation/templates.rst

Lines changed: 0 additions & 89 deletions
This file was deleted.

0 commit comments

Comments
 (0)