Skip to content

Commit dcb9f7e

Browse files
committed
minor #18924 [Translation] Add information about custom providers (SzymonKaminski)
This PR was merged into the 5.4 branch. Discussion ---------- [Translation] Add information about custom providers PR adds `translation.provider_factory` to list of DIC tags. I've also added short section about creating custom provider in main Translation file. Commits ------- 14544f8 [Translation] Add information about custom providers
2 parents 42b734d + 14544f8 commit dcb9f7e

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

reference/dic_tags.rst

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,50 @@ This is the name that's used to determine which dumper should be used.
12281228
$container->register(JsonFileDumper::class)
12291229
->addTag('translation.dumper', ['alias' => 'json']);
12301230
1231+
.. _reference-dic-tags-translation-provider-factory:
1232+
1233+
translation.provider_factory
1234+
----------------------------
1235+
1236+
**Purpose**: To register a factory creating custom Translation Provider
1237+
1238+
Register your factory as a service and tag it with ``translation.provider_factory``:
1239+
1240+
.. configuration-block::
1241+
1242+
.. code-block:: yaml
1243+
1244+
services:
1245+
App\Translation\CustomProviderFactory:
1246+
tags:
1247+
- { name: translation.provider_factory }
1248+
1249+
.. code-block:: xml
1250+
1251+
<?xml version="1.0" encoding="UTF-8" ?>
1252+
<container xmlns="http://symfony.com/schema/dic/services"
1253+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1254+
xsi:schemaLocation="http://symfony.com/schema/dic/services
1255+
https://symfony.com/schema/dic/services/services-1.0.xsd">
1256+
1257+
<services>
1258+
<service id="App\Translation\CustomProviderFactory">
1259+
<tag name="translation.provider_factory"/>
1260+
</service>
1261+
</services>
1262+
</container>
1263+
1264+
.. code-block:: php
1265+
1266+
use App\Translation\CustomProviderFactory;
1267+
1268+
$container
1269+
->register(CustomProviderFactory::class)
1270+
->addTag('translation.provider_factory')
1271+
;
1272+
1273+
For more details, see :ref:`translation providers <translation-providers>`.
1274+
12311275
.. _reference-dic-tags-twig-extension:
12321276

12331277
twig.extension

translation.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,17 @@ now use the following commands to push (upload) and pull (download) translations
746746
# check out the command help to see its options (format, domains, locales, intl-icu, etc.)
747747
$ php bin/console translation:pull --help
748748
749+
Creating custom Provider
750+
~~~~~~~~~~~~~~~~~~~~~~~~
751+
752+
If you wish to create new Translation Provider, you need to create two classes.
753+
First one must implement :class:`Symfony\\Component\\Translation\\Provider\\ProviderInterface`.
754+
Second needs to be a factory, that will create instances of the first class. It must implement
755+
:class:`Symfony\\Component\\Translation\\Provider\\ProviderFactoryInterface`.
756+
You may extend :class:`Symfony\\Component\\Translation\\Provider\\AbstractProviderFactory`
757+
to simplify its creation. Additionally you need to tag the factory service with
758+
:ref:`translation.provider_factory <reference-dic-tags-translation-provider-factory>`.
759+
749760
.. _translation-locale:
750761

751762
Handling the User's Locale

0 commit comments

Comments
 (0)