Skip to content

[Translation] Add information about custom providers #18924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions reference/dic_tags.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,50 @@ This is the name that's used to determine which dumper should be used.
$container->register(JsonFileDumper::class)
->addTag('translation.dumper', ['alias' => 'json']);

.. _reference-dic-tags-translation-provider-factory:

translation.provider_factory
----------------------------

**Purpose**: To register a factory creating custom Translation Provider

Register your factory as a service and tag it with ``translation.provider_factory``:

.. configuration-block::

.. code-block:: yaml

services:
App\Translation\CustomProviderFactory:
tags:
- { name: translation.provider_factory }

.. code-block:: xml

<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="App\Translation\CustomProviderFactory">
<tag name="translation.provider_factory"/>
</service>
</services>
</container>

.. code-block:: php

use App\Translation\CustomProviderFactory;

$container
->register(CustomProviderFactory::class)
->addTag('translation.provider_factory')
;

For more details, see :ref:`translation providers <translation-providers>`.

.. _reference-dic-tags-twig-extension:

twig.extension
Expand Down
11 changes: 11 additions & 0 deletions translation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,17 @@ now use the following commands to push (upload) and pull (download) translations
# check out the command help to see its options (format, domains, locales, intl-icu, etc.)
$ php bin/console translation:pull --help

Creating custom Provider
~~~~~~~~~~~~~~~~~~~~~~~~

If you wish to create new Translation Provider, you need to create two classes.
First one must implement :class:`Symfony\\Component\\Translation\\Provider\\ProviderInterface`.
Second needs to be a factory, that will create instances of the first class. It must implement
:class:`Symfony\\Component\\Translation\\Provider\\ProviderFactoryInterface`.
You may extend :class:`Symfony\\Component\\Translation\\Provider\\AbstractProviderFactory`
to simplify its creation. Additionally you need to tag the factory service with
:ref:`translation.provider_factory <reference-dic-tags-translation-provider-factory>`.

.. _translation-locale:

Handling the User's Locale
Expand Down