@@ -51,21 +51,31 @@ to learn even more. Overall, the process has several steps:
51
51
Configuration
52
52
-------------
53
53
54
- Translations are handled by a ``translator `` service that uses the user's
55
- locale to lookup and return translated messages. Before using it, enable the
56
- ``translator `` in your configuration:
54
+ In a :doc: `Symfony Flex </setup/flex >` based application, run this command to
55
+ add translation support:
56
+
57
+ .. code-block :: terminal
58
+
59
+ $ composer require translator
60
+
61
+ This command creates an initial config file where you can define the default
62
+ locale of the app and the :ref: `fallback locales <translation-fallback >` used
63
+ when Symfony can't find some translation:
57
64
58
65
.. configuration-block ::
59
66
60
67
.. code-block :: yaml
61
68
62
- # app/ config/config.yml
69
+ # config/packages/translation.yaml
63
70
framework :
64
- translator : { fallbacks: [en] }
71
+ default_locale : ' en'
72
+ translator :
73
+ fallbacks : ['en']
74
+ # ...
65
75
66
76
.. code-block :: xml
67
77
68
- <!-- app/ config/config .xml -->
78
+ <!-- config/packages/translation .xml -->
69
79
<?xml version =" 1.0" encoding =" UTF-8" ?>
70
80
<container xmlns =" http://symfony.com/schema/dic/services"
71
81
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
@@ -75,23 +85,23 @@ locale to lookup and return translated messages. Before using it, enable the
75
85
http://symfony.com/schema/dic/symfony
76
86
http://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
77
87
78
- <framework : config >
88
+ <framework : config default-locale = " en " >
79
89
<framework : translator >
80
90
<framework : fallback >en</framework : fallback >
91
+ <!-- ... -->
81
92
</framework : translator >
82
93
</framework : config >
83
94
</container >
84
95
85
96
.. code-block :: php
86
97
87
- // app/ config/config .php
98
+ // config/packages/translation .php
88
99
$container->loadFromExtension('framework', array(
100
+ 'default_locale' => 'en',
89
101
'translator' => array('fallbacks' => array('en')),
102
+ // ...
90
103
));
91
104
92
- See :ref: `translation-fallback ` for details on the ``fallbacks `` key
93
- and what Symfony does when it doesn't find a translation.
94
-
95
105
The locale used in translations is the one stored on the request. This is
96
106
typically set via a ``_locale `` attribute on your routes (see :ref: `translation-locale-url `).
97
107
@@ -108,10 +118,11 @@ for example, that you're translating a simple message from inside a controller::
108
118
109
119
// ...
110
120
use Symfony\Component\HttpFoundation\Response;
121
+ use Symfony\Component\Translation\Translator;
111
122
112
- public function indexAction( )
123
+ public function index(Translator $translator )
113
124
{
114
- $translated = $this->get(' translator') ->trans('Symfony is great');
125
+ $translated = $translator->trans('Symfony is great');
115
126
116
127
return new Response($translated);
117
128
}
@@ -129,7 +140,7 @@ different formats, XLIFF being the recommended format:
129
140
130
141
.. code-block :: xml
131
142
132
- <!-- messages.fr.xlf -->
143
+ <!-- translations/ messages.fr.xlf -->
133
144
<?xml version =" 1.0" ?>
134
145
<xliff version =" 1.2" xmlns =" urn:oasis:names:tc:xliff:document:1.2" >
135
146
<file source-language =" en" datatype =" plaintext" original =" file.ext" >
@@ -144,12 +155,12 @@ different formats, XLIFF being the recommended format:
144
155
145
156
.. code-block :: yaml
146
157
147
- # messages.fr.yml
158
+ # translations/ messages.fr.yml
148
159
Symfony is great : J'aime Symfony
149
160
150
161
.. code-block :: php
151
162
152
- // messages.fr.php
163
+ // translations/ messages.fr.php
153
164
return array(
154
165
'Symfony is great' => 'J\'aime Symfony',
155
166
);
@@ -186,10 +197,11 @@ Message Placeholders
186
197
Sometimes, a message containing a variable needs to be translated::
187
198
188
199
use Symfony\Component\HttpFoundation\Response;
200
+ use Symfony\Component\Translation\Translator;
189
201
190
- public function indexAction( $name)
202
+ public function index(Translator $translator, $name)
191
203
{
192
- $translated = $this->get(' translator') ->trans('Hello '.$name);
204
+ $translated = $translator->trans('Hello '.$name);
193
205
194
206
return new Response($translated);
195
207
}
@@ -336,14 +348,14 @@ Translation Resource/File Names and Locations
336
348
337
349
Symfony looks for message files (i.e. translations) in the following default locations:
338
350
339
- * the ``app/Resources/ translations `` directory;
351
+ * the ``translations/ `` directory;
340
352
341
- * the ``app /Resources/<bundle name>/translations `` directory;
353
+ * the ``src /Resources/<bundle name>/translations/ `` directory;
342
354
343
355
* the ``Resources/translations/ `` directory inside of any bundle.
344
356
345
357
The locations are listed here with the highest priority first. That is, you can
346
- override the translation messages of a bundle in any of the top 2 directories.
358
+ override the translation messages of a bundle in any of the top two directories.
347
359
348
360
The override mechanism works at a key level: only the overridden keys need
349
361
to be listed in a higher priority message file. When a key is not found
@@ -359,14 +371,14 @@ must be named according to the following path: ``domain.locale.loader``:
359
371
* **locale **: The locale that the translations are for (e.g. ``en_GB ``, ``en ``, etc);
360
372
361
373
* **loader **: How Symfony should load and parse the file (e.g. ``xlf ``,
362
- ``php ``, ``yml ``, etc).
374
+ ``php ``, ``yaml ``, etc).
363
375
364
376
The loader can be the name of any registered loader. By default, Symfony
365
377
provides many loaders, including:
366
378
367
379
* ``xlf ``: XLIFF file;
368
380
* ``php ``: PHP file;
369
- * ``yml ``: YAML file.
381
+ * ``yaml ``: YAML file.
370
382
371
383
The choice of which loader to use is entirely up to you and is a matter of
372
384
taste. The recommended option is to use ``xlf `` for translations.
@@ -381,15 +393,15 @@ For more options, see :ref:`component-translator-message-catalogs`.
381
393
382
394
.. code-block :: yaml
383
395
384
- # app/ config/config.yml
396
+ # config/packages/translation.yaml
385
397
framework :
386
398
translator :
387
399
paths :
388
- - ' %kernel.project_dir%/translations'
400
+ - ' %kernel.project_dir%/custom/path/to/ translations'
389
401
390
402
.. code-block :: xml
391
403
392
- <!-- app/ config/config .xml -->
404
+ <!-- config/packages/translation .xml -->
393
405
<?xml version =" 1.0" encoding =" UTF-8" ?>
394
406
<container xmlns =" http://symfony.com/schema/dic/services"
395
407
xmlns : framework =" http://symfony.com/schema/dic/symfony"
@@ -402,18 +414,18 @@ For more options, see :ref:`component-translator-message-catalogs`.
402
414
403
415
<framework : config >
404
416
<framework : translator >
405
- <framework : path >%kernel.project_dir%/translations</framework : path >
417
+ <framework : path >%kernel.project_dir%/custom/path/to/ translations</framework : path >
406
418
</framework : translator >
407
419
</framework : config >
408
420
</container >
409
421
410
422
.. code-block :: php
411
423
412
- // app/ config/config .php
424
+ // config/packages/translation .php
413
425
$container->loadFromExtension('framework', array(
414
426
'translator' => array(
415
427
'paths' => array(
416
- '%kernel.project_dir%/translations',
428
+ '%kernel.project_dir%/custom/path/to/ translations',
417
429
),
418
430
),
419
431
));
@@ -455,7 +467,7 @@ checks translation resources for several locales:
455
467
456
468
.. note ::
457
469
458
- When Symfony doesn 't find a translation in the given locale, it will
470
+ When Symfony can 't find a translation in the given locale, it will
459
471
add the missing translation to the log file. For details,
460
472
see :ref: `reference-framework-translator-logging `.
461
473
@@ -504,9 +516,10 @@ Learn more
504
516
505
517
.. toctree ::
506
518
:maxdepth: 1
507
- :glob:
508
519
509
- /translation/*
520
+ translation/locale
521
+ translation/debug
522
+ translation/lint
510
523
511
524
.. _`i18n` : https://en.wikipedia.org/wiki/Internationalization_and_localization
512
525
.. _`ISO 3166-1 alpha-2` : https://en.wikipedia.org/wiki/ISO_3166-1#Current_codes
0 commit comments