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