@@ -12,13 +12,11 @@ wrapping each with a function capable of translating the text (or "message")
12
12
into the language of the user::
13
13
14
14
// text will *always* print out in English
15
- dump('Hello World');
16
- die();
15
+ echo 'Hello World';
17
16
18
17
// text can be translated into the end-user's language or
19
18
// default to English
20
- dump($translator->trans('Hello World'));
21
- die();
19
+ echo $translator->trans('Hello World');
22
20
23
21
.. note ::
24
22
@@ -163,15 +161,15 @@ different formats, XLIFF being the recommended format:
163
161
164
162
// translations/messages.fr.php
165
163
return [
166
- 'Symfony is great' => 'J\ 'aime Symfony' ,
164
+ 'Symfony is great' => "J 'aime Symfony" ,
167
165
];
168
166
169
167
For information on where these files should be located, see
170
168
:ref: `translation-resource-locations `.
171
169
172
170
Now, if the language of the user's locale is French (e.g. ``fr_FR `` or ``fr_BE ``),
173
171
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 > `.
175
173
176
174
The Translation Process
177
175
~~~~~~~~~~~~~~~~~~~~~~~
@@ -184,7 +182,8 @@ To actually translate the message, Symfony uses the following process:
184
182
resources defined for the ``locale `` (e.g. ``fr_FR ``). Messages from the
185
183
:ref: `fallback locale <translation-fallback >` are also loaded and
186
184
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.
188
187
189
188
* If the message is located in the catalog, the translation is returned. If
190
189
not, the translator returns the original message.
@@ -240,112 +239,14 @@ Translations in Templates
240
239
-------------------------
241
240
242
241
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:
244
243
245
- .. _ translation-tags :
244
+ .. code-block :: html+twig
246
245
247
- Twig Templates
248
- ~~~~~~~~~~~~~~
246
+ <h1>{% trans %}Symfony is great!{% endtrans %}</h1>
249
247
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.
349
250
350
251
Extracting Translation Contents and Updating Catalogs Automatically
351
252
-------------------------------------------------------------------
@@ -381,11 +282,9 @@ Translation Resource/File Names and Locations
381
282
382
283
Symfony looks for message files (i.e. translations) in the following default locations:
383
284
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.
389
288
390
289
.. deprecated :: 4.2
391
290
@@ -486,6 +385,12 @@ For more options, see :ref:`component-translator-message-catalogs`.
486
385
487
386
$ php bin/console cache:clear
488
387
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
+
489
394
.. _translation-fallback :
490
395
491
396
Fallback Translation Locales
@@ -510,12 +415,6 @@ checks translation resources for several locales:
510
415
add the missing translation to the log file. For details,
511
416
see :ref: `reference-framework-translator-logging `.
512
417
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
-
519
418
Translating Database Content
520
419
----------------------------
521
420
@@ -555,6 +454,7 @@ Learn more
555
454
.. toctree ::
556
455
:maxdepth: 1
557
456
457
+ translation/templates
558
458
translation/locale
559
459
translation/debug
560
460
translation/lint
0 commit comments