Skip to content

Commit 8fe3cf0

Browse files
committed
Updated the translation/* articles to Symfony 4
1 parent a1901ff commit 8fe3cf0

File tree

8 files changed

+143
-94
lines changed

8 files changed

+143
-94
lines changed

_images/translation/debug_1.png

-22.2 KB
Binary file not shown.

_images/translation/debug_2.png

-22 KB
Binary file not shown.

_images/translation/debug_3.png

-21.9 KB
Binary file not shown.

_images/translation/debug_4.png

-21.7 KB
Binary file not shown.

translation.rst

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,31 @@ to learn even more. Overall, the process has several steps:
5151
Configuration
5252
-------------
5353

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:
5764

5865
.. configuration-block::
5966

6067
.. code-block:: yaml
6168
62-
# app/config/config.yml
69+
# config/packages/translation.yaml
6370
framework:
64-
translator: { fallbacks: [en] }
71+
default_locale: 'en'
72+
translator:
73+
fallbacks: ['en']
74+
# ...
6575
6676
.. code-block:: xml
6777
68-
<!-- app/config/config.xml -->
78+
<!-- config/packages/translation.xml -->
6979
<?xml version="1.0" encoding="UTF-8" ?>
7080
<container xmlns="http://symfony.com/schema/dic/services"
7181
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
7585
http://symfony.com/schema/dic/symfony
7686
http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
7787
78-
<framework:config>
88+
<framework:config default-locale="en">
7989
<framework:translator>
8090
<framework:fallback>en</framework:fallback>
91+
<!-- ... -->
8192
</framework:translator>
8293
</framework:config>
8394
</container>
8495
8596
.. code-block:: php
8697
87-
// app/config/config.php
98+
// config/packages/translation.php
8899
$container->loadFromExtension('framework', array(
100+
'default_locale' => 'en',
89101
'translator' => array('fallbacks' => array('en')),
102+
// ...
90103
));
91104
92-
See :ref:`translation-fallback` for details on the ``fallbacks`` key
93-
and what Symfony does when it doesn't find a translation.
94-
95105
The locale used in translations is the one stored on the request. This is
96106
typically set via a ``_locale`` attribute on your routes (see :ref:`translation-locale-url`).
97107

@@ -108,10 +118,11 @@ for example, that you're translating a simple message from inside a controller::
108118

109119
// ...
110120
use Symfony\Component\HttpFoundation\Response;
121+
use Symfony\Component\Translation\Translator;
111122

112-
public function indexAction()
123+
public function index(Translator $translator)
113124
{
114-
$translated = $this->get('translator')->trans('Symfony is great');
125+
$translated = $translator->trans('Symfony is great');
115126

116127
return new Response($translated);
117128
}
@@ -129,7 +140,7 @@ different formats, XLIFF being the recommended format:
129140

130141
.. code-block:: xml
131142
132-
<!-- messages.fr.xlf -->
143+
<!-- translations/messages.fr.xlf -->
133144
<?xml version="1.0"?>
134145
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
135146
<file source-language="en" datatype="plaintext" original="file.ext">
@@ -144,12 +155,12 @@ different formats, XLIFF being the recommended format:
144155
145156
.. code-block:: yaml
146157
147-
# messages.fr.yml
158+
# translations/messages.fr.yml
148159
Symfony is great: J'aime Symfony
149160
150161
.. code-block:: php
151162
152-
// messages.fr.php
163+
// translations/messages.fr.php
153164
return array(
154165
'Symfony is great' => 'J\'aime Symfony',
155166
);
@@ -186,10 +197,11 @@ Message Placeholders
186197
Sometimes, a message containing a variable needs to be translated::
187198

188199
use Symfony\Component\HttpFoundation\Response;
200+
use Symfony\Component\Translation\Translator;
189201

190-
public function indexAction($name)
202+
public function index(Translator $translator, $name)
191203
{
192-
$translated = $this->get('translator')->trans('Hello '.$name);
204+
$translated = $translator->trans('Hello '.$name);
193205

194206
return new Response($translated);
195207
}
@@ -336,14 +348,14 @@ Translation Resource/File Names and Locations
336348

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

339-
* the ``app/Resources/translations`` directory;
351+
* the ``translations/`` directory;
340352

341-
* the ``app/Resources/<bundle name>/translations`` directory;
353+
* the ``src/Resources/<bundle name>/translations/`` directory;
342354

343355
* the ``Resources/translations/`` directory inside of any bundle.
344356

345357
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.
347359

348360
The override mechanism works at a key level: only the overridden keys need
349361
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``:
359371
* **locale**: The locale that the translations are for (e.g. ``en_GB``, ``en``, etc);
360372

361373
* **loader**: How Symfony should load and parse the file (e.g. ``xlf``,
362-
``php``, ``yml``, etc).
374+
``php``, ``yaml``, etc).
363375

364376
The loader can be the name of any registered loader. By default, Symfony
365377
provides many loaders, including:
366378

367379
* ``xlf``: XLIFF file;
368380
* ``php``: PHP file;
369-
* ``yml``: YAML file.
381+
* ``yaml``: YAML file.
370382

371383
The choice of which loader to use is entirely up to you and is a matter of
372384
taste. The recommended option is to use ``xlf`` for translations.
@@ -381,15 +393,15 @@ For more options, see :ref:`component-translator-message-catalogs`.
381393

382394
.. code-block:: yaml
383395
384-
# app/config/config.yml
396+
# config/packages/translation.yaml
385397
framework:
386398
translator:
387399
paths:
388-
- '%kernel.project_dir%/translations'
400+
- '%kernel.project_dir%/custom/path/to/translations'
389401
390402
.. code-block:: xml
391403
392-
<!-- app/config/config.xml -->
404+
<!-- config/packages/translation.xml -->
393405
<?xml version="1.0" encoding="UTF-8" ?>
394406
<container xmlns="http://symfony.com/schema/dic/services"
395407
xmlns:framework="http://symfony.com/schema/dic/symfony"
@@ -402,18 +414,18 @@ For more options, see :ref:`component-translator-message-catalogs`.
402414
403415
<framework:config>
404416
<framework:translator>
405-
<framework:path>%kernel.project_dir%/translations</framework:path>
417+
<framework:path>%kernel.project_dir%/custom/path/to/translations</framework:path>
406418
</framework:translator>
407419
</framework:config>
408420
</container>
409421
410422
.. code-block:: php
411423
412-
// app/config/config.php
424+
// config/packages/translation.php
413425
$container->loadFromExtension('framework', array(
414426
'translator' => array(
415427
'paths' => array(
416-
'%kernel.project_dir%/translations',
428+
'%kernel.project_dir%/custom/path/to/translations',
417429
),
418430
),
419431
));
@@ -455,7 +467,7 @@ checks translation resources for several locales:
455467

456468
.. note::
457469

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
459471
add the missing translation to the log file. For details,
460472
see :ref:`reference-framework-translator-logging`.
461473

@@ -504,9 +516,10 @@ Learn more
504516

505517
.. toctree::
506518
:maxdepth: 1
507-
:glob:
508519

509-
/translation/*
520+
translation/locale
521+
translation/debug
522+
translation/lint
510523

511524
.. _`i18n`: https://en.wikipedia.org/wiki/Internationalization_and_localization
512525
.. _`ISO 3166-1 alpha-2`: https://en.wikipedia.org/wiki/ISO_3166-1#Current_codes

0 commit comments

Comments
 (0)