Skip to content

Commit 04e00d9

Browse files
committed
Merge branch '5.4' into 6.3
* 5.4: [Translation] Add information about custom providers
2 parents a345d4c + 6b2e218 commit 04e00d9

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

reference/dic_tags.rst

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,49 @@ This is the name that's used to determine which dumper should be used.
12081208
$container->register(JsonFileDumper::class)
12091209
->addTag('translation.dumper', ['alias' => 'json']);
12101210
1211+
.. _reference-dic-tags-translation-provider-factory:
1212+
1213+
translation.provider_factory
1214+
----------------------------
1215+
1216+
**Purpose**: to register a factory related to custom translation providers
1217+
1218+
When creating custom :ref:`translation providers <translation-providers>`, you
1219+
must register your factory as a service and tag it with ``translation.provider_factory``:
1220+
1221+
.. configuration-block::
1222+
1223+
.. code-block:: yaml
1224+
1225+
services:
1226+
App\Translation\CustomProviderFactory:
1227+
tags:
1228+
- { name: translation.provider_factory }
1229+
1230+
.. code-block:: xml
1231+
1232+
<?xml version="1.0" encoding="UTF-8" ?>
1233+
<container xmlns="http://symfony.com/schema/dic/services"
1234+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1235+
xsi:schemaLocation="http://symfony.com/schema/dic/services
1236+
https://symfony.com/schema/dic/services/services-1.0.xsd">
1237+
1238+
<services>
1239+
<service id="App\Translation\CustomProviderFactory">
1240+
<tag name="translation.provider_factory"/>
1241+
</service>
1242+
</services>
1243+
</container>
1244+
1245+
.. code-block:: php
1246+
1247+
use App\Translation\CustomProviderFactory;
1248+
1249+
$container
1250+
->register(CustomProviderFactory::class)
1251+
->addTag('translation.provider_factory')
1252+
;
1253+
12111254
.. _reference-dic-tags-twig-extension:
12121255

12131256
twig.extension

translation.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,19 @@ now use the following commands to push (upload) and pull (download) translations
743743
# check out the command help to see its options (format, domains, locales, intl-icu, etc.)
744744
$ php bin/console translation:pull --help
745745
746+
Creating Custom Providers
747+
~~~~~~~~~~~~~~~~~~~~~~~~~
748+
749+
In addition to using Symfony's built-in translation providers, you can create
750+
your own providers. To do so, you need to create two classes:
751+
752+
#. The first class must implement :class:`Symfony\\Component\\Translation\\Provider\\ProviderInterface`;
753+
#. The second class needs to be a factory which will create instances of the first class. It must implement
754+
:class:`Symfony\\Component\\Translation\\Provider\\ProviderFactoryInterface` (you can extend :class:`Symfony\\Component\\Translation\\Provider\\AbstractProviderFactory` to simplify its creation).
755+
756+
After creating these two classes, you need to register your factory as a service
757+
and tag it with :ref:`translation.provider_factory <reference-dic-tags-translation-provider-factory>`.
758+
746759
.. _translation-locale:
747760

748761
Handling the User's Locale

0 commit comments

Comments
 (0)