@@ -12,13 +12,11 @@ wrapping each with a function capable of translating the text (or "message")
1212into 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
172170Now, if the language of the user's locale is French (e.g. ``fr_FR `` or ``fr_BE ``),
173171the 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
176174The 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
242241Most 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
350251Extracting Translation Contents and Updating Catalogs Automatically
351252-------------------------------------------------------------------
@@ -381,11 +282,9 @@ Translation Resource/File Names and Locations
381282
382283Symfony 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
491396Fallback 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-
519418Translating 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
0 commit comments