Skip to content

Commit a6dabc1

Browse files
committed
Some general fixes/improvements for translation guide
1 parent 737cc7f commit a6dabc1

File tree

2 files changed

+132
-121
lines changed

2 files changed

+132
-121
lines changed

translation.rst

Lines changed: 21 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ wrapping each with a function capable of translating the text (or "message")
1212
into the language of the user::
1313

1414
// text will *always* print out in English
15-
dump('Hello World');
16-
die();
15+
echo 'Hello World';
1716

1817
// text can be translated into the end-user's language or
1918
// default to English
20-
dump($translator->trans('Hello World'));
21-
die();
19+
echo $translator->trans('Hello World');
2220

2321
.. note::
2422

@@ -163,15 +161,15 @@ different formats, XLIFF being the recommended format:
163161
164162
// translations/messages.fr.php
165163
return [
166-
'Symfony is great' => 'J\'aime Symfony',
164+
'Symfony is great' => "J'aime Symfony",
167165
];
168166
169167
For information on where these files should be located, see
170168
:ref:`translation-resource-locations`.
171169

172170
Now, if the language of the user's locale is French (e.g. ``fr_FR`` or ``fr_BE``),
173171
the message will be translated into ``J'aime Symfony``. You can also translate
174-
the message inside your :ref:`templates <translation-tags>`.
172+
the message inside your `templates <Translations in Templates>`.
175173

176174
The Translation Process
177175
~~~~~~~~~~~~~~~~~~~~~~~
@@ -184,7 +182,8 @@ To actually translate the message, Symfony uses the following process:
184182
resources defined for the ``locale`` (e.g. ``fr_FR``). Messages from the
185183
:ref:`fallback locale <translation-fallback>` are also loaded and
186184
added to the catalog if they don't already exist. The end result is a large
187-
"dictionary" of translations.
185+
"dictionary" of translations. This catalog is cached in production, to
186+
minimize performance impact.
188187

189188
* If the message is located in the catalog, the translation is returned. If
190189
not, the translator returns the original message.
@@ -240,112 +239,14 @@ Translations in Templates
240239
-------------------------
241240

242241
Most of the time, translation occurs in templates. Symfony provides native
243-
support for both Twig and PHP templates.
242+
support for both Twig and PHP templates:
244243

245-
.. _translation-tags:
244+
.. code-block:: html+twig
246245

247-
Twig Templates
248-
~~~~~~~~~~~~~~
246+
<h1>{% trans %}Symfony is great!{% endtrans %}</h1>
249247

250-
Symfony provides specialized Twig tags (``trans`` and ``transchoice``) to
251-
help with message translation of *static blocks of text*:
252-
253-
.. code-block:: twig
254-
255-
{% trans %}Hello %name%{% endtrans %}
256-
257-
{% transchoice count %}
258-
{0} There are no apples|{1} There is one apple|]1,Inf[ There are %count% apples
259-
{% endtranschoice %}
260-
261-
The ``transchoice`` tag automatically gets the ``%count%`` variable from
262-
the current context and passes it to the translator. This mechanism only
263-
works when you use a placeholder following the ``%var%`` pattern.
264-
265-
.. caution::
266-
267-
The ``%var%`` notation of placeholders is required when translating in
268-
Twig templates using the tag.
269-
270-
.. tip::
271-
272-
If you need to use the percent character (``%``) in a string, escape it by
273-
doubling it: ``{% trans %}Percent: %percent%%%{% endtrans %}``
274-
275-
You can also specify the message domain and pass some additional variables:
276-
277-
.. code-block:: twig
278-
279-
{% trans with {'%name%': 'Fabien'} from 'app' %}Hello %name%{% endtrans %}
280-
281-
{% trans with {'%name%': 'Fabien'} from 'app' into 'fr' %}Hello %name%{% endtrans %}
282-
283-
{% transchoice count with {'%name%': 'Fabien'} from 'app' %}
284-
{0} %name%, there are no apples|{1} %name%, there is one apple|]1,Inf[ %name%, there are %count% apples
285-
{% endtranschoice %}
286-
287-
.. _translation-filters:
288-
289-
The ``trans`` and ``transchoice`` filters can be used to translate *variable
290-
texts* and complex expressions:
291-
292-
.. code-block:: twig
293-
294-
{{ message|trans }}
295-
296-
{{ message|transchoice(5) }}
297-
298-
{{ message|trans({'%name%': 'Fabien'}, 'app') }}
299-
300-
{{ message|transchoice(5, {'%name%': 'Fabien'}, 'app') }}
301-
302-
.. tip::
303-
304-
Using the translation tags or filters have the same effect, but with
305-
one subtle difference: automatic output escaping is only applied to
306-
translations using a filter. In other words, if you need to be sure
307-
that your translated message is *not* output escaped, you must apply
308-
the ``raw`` filter after the translation filter:
309-
310-
.. code-block:: html+twig
311-
312-
{# text translated between tags is never escaped #}
313-
{% trans %}
314-
<h3>foo</h3>
315-
{% endtrans %}
316-
317-
{% set message = '<h3>foo</h3>' %}
318-
319-
{# strings and variables translated via a filter are escaped by default #}
320-
{{ message|trans|raw }}
321-
{{ '<h3>bar</h3>'|trans|raw }}
322-
323-
.. tip::
324-
325-
You can set the translation domain for an entire Twig template with a single tag:
326-
327-
.. code-block:: twig
328-
329-
{% trans_default_domain 'app' %}
330-
331-
Note that this only influences the current template, not any "included"
332-
template (in order to avoid side effects).
333-
334-
PHP Templates
335-
~~~~~~~~~~~~~
336-
337-
The translator service is accessible in PHP templates through the
338-
``translator`` helper:
339-
340-
.. code-block:: html+php
341-
342-
<?= $view['translator']->trans('Symfony is great') ?>
343-
344-
<?= $view['translator']->transChoice(
345-
'{0} There are no apples|{1} There is one apple|]1,Inf[ There are %count% apples',
346-
10,
347-
['%count%' => 10]
348-
) ?>
248+
Read :doc:`/translation/templates` for more information about the Twig tags and
249+
filters for translation.
349250

350251
Extracting Translation Contents and Updating Catalogs Automatically
351252
-------------------------------------------------------------------
@@ -381,11 +282,9 @@ Translation Resource/File Names and Locations
381282

382283
Symfony looks for message files (i.e. translations) in the following default locations:
383284

384-
* the ``translations/`` directory (at the root of the project);
385-
386-
* the ``src/Resources/<bundle name>/translations/`` directory;
387-
388-
* the ``Resources/translations/`` directory inside of any bundle.
285+
#. the ``translations/`` directory (at the root of the project);
286+
#. the ``src/Resources/<bundle name>/translations/`` directory;
287+
#. the ``Resources/translations/`` directory inside of any bundle.
389288

390289
.. deprecated:: 4.2
391290

@@ -486,6 +385,12 @@ For more options, see :ref:`component-translator-message-catalogs`.
486385
487386
$ php bin/console cache:clear
488387
388+
Handling the User's Locale
389+
--------------------------
390+
391+
Translating happens based on the user's locale. Read :doc:`/translation/locale`
392+
to learn more about how to handle it.
393+
489394
.. _translation-fallback:
490395

491396
Fallback Translation Locales
@@ -510,12 +415,6 @@ checks translation resources for several locales:
510415
add the missing translation to the log file. For details,
511416
see :ref:`reference-framework-translator-logging`.
512417

513-
Handling the User's Locale
514-
--------------------------
515-
516-
Translating happens based on the user's locale. Read :doc:`/translation/locale`
517-
to learn more about how to handle it.
518-
519418
Translating Database Content
520419
----------------------------
521420

@@ -555,6 +454,7 @@ Learn more
555454
.. toctree::
556455
:maxdepth: 1
557456

457+
translation/templates
558458
translation/locale
559459
translation/debug
560460
translation/lint

translation/templates.rst

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
Using Translation in Templates
2+
==============================
3+
4+
Twig Templates
5+
--------------
6+
7+
.. _translation-tags:
8+
9+
Using Twig Tags
10+
~~~~~~~~~~~~~~~
11+
12+
Symfony provides specialized Twig tags (``trans`` and ``transchoice``) to
13+
help with message translation of *static blocks of text*:
14+
15+
.. code-block:: twig
16+
17+
{% trans %}Hello %name%{% endtrans %}
18+
19+
{% transchoice count %}
20+
{0} There are no apples|{1} There is one apple|]1,Inf[ There are %count% apples
21+
{% endtranschoice %}
22+
23+
The ``transchoice`` tag automatically gets the ``%count%`` variable from
24+
the current context and passes it to the translator. This mechanism only
25+
works when you use a placeholder following the ``%var%`` pattern.
26+
27+
.. caution::
28+
29+
The ``%var%`` notation of placeholders is required when translating in
30+
Twig templates using the tag.
31+
32+
.. tip::
33+
34+
If you need to use the percent character (``%``) in a string, escape it by
35+
doubling it: ``{% trans %}Percent: %percent%%%{% endtrans %}``
36+
37+
You can also specify the message domain and pass some additional variables:
38+
39+
.. code-block:: twig
40+
41+
{% trans with {'%name%': 'Fabien'} from 'app' %}Hello %name%{% endtrans %}
42+
43+
{% trans with {'%name%': 'Fabien'} from 'app' into 'fr' %}Hello %name%{% endtrans %}
44+
45+
{% transchoice count with {'%name%': 'Fabien'} from 'app' %}
46+
{0} %name%, there are no apples|{1} %name%, there is one apple|]1,Inf[ %name%, there are %count% apples
47+
{% endtranschoice %}
48+
49+
.. _translation-filters:
50+
51+
Using Twig Filters
52+
~~~~~~~~~~~~~~~~~~
53+
54+
The ``trans`` and ``transchoice`` filters can be used to translate *variable
55+
texts* and complex expressions:
56+
57+
.. code-block:: twig
58+
59+
{{ message|trans }}
60+
61+
{{ message|transchoice(5) }}
62+
63+
{{ message|trans({'%name%': 'Fabien'}, 'app') }}
64+
65+
{{ message|transchoice(5, {'%name%': 'Fabien'}, 'app') }}
66+
67+
.. tip::
68+
69+
Using the translation tags or filters have the same effect, but with
70+
one subtle difference: automatic output escaping is only applied to
71+
translations using a filter. In other words, if you need to be sure
72+
that your translated message is *not* output escaped, you must apply
73+
the ``raw`` filter after the translation filter:
74+
75+
.. code-block:: html+twig
76+
77+
{# text translated between tags is never escaped #}
78+
{% trans %}
79+
<h3>foo</h3>
80+
{% endtrans %}
81+
82+
{% set message = '<h3>foo</h3>' %}
83+
84+
{# strings and variables translated via a filter are escaped by default #}
85+
{{ message|trans|raw }}
86+
{{ '<h3>bar</h3>'|trans|raw }}
87+
88+
.. tip::
89+
90+
You can set the translation domain for an entire Twig template with a single tag:
91+
92+
.. code-block:: twig
93+
94+
{% trans_default_domain 'app' %}
95+
96+
Note that this only influences the current template, not any "included"
97+
template (in order to avoid side effects).
98+
99+
PHP Templates
100+
-------------
101+
102+
The translator service is accessible in PHP templates through the
103+
``translator`` helper::
104+
105+
<?= $view['translator']->trans('Symfony is great') ?>
106+
107+
<?= $view['translator']->transChoice(
108+
'{0} There are no apples|{1} There is one apple|]1,Inf[ There are %count% apples',
109+
10,
110+
['%count%' => 10]
111+
) ?>

0 commit comments

Comments
 (0)