From c1ab456abfbce08e78825ae252d970ed7d239c69 Mon Sep 17 00:00:00 2001 From: Guillaume Loulier Date: Wed, 23 Mar 2022 16:13:51 +0100 Subject: [PATCH] refactor(reference): document kernel.locale_aware --- reference/dic_tags.rst | 71 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/reference/dic_tags.rst b/reference/dic_tags.rst index 28d11c3c392..942b7fc7134 100644 --- a/reference/dic_tags.rst +++ b/reference/dic_tags.rst @@ -453,6 +453,77 @@ To add a new rendering strategy - in addition to the core strategies like :class:`Symfony\\Component\\HttpKernel\\Fragment\\FragmentRendererInterface`, register it as a service, then tag it with ``kernel.fragment_renderer``. +kernel.locale_aware +------------------- + +.. versionadded:: 4.3 + + The ``kernel.locale_aware`` tag was introduced in Symfony 4.3. + +**Purpose**: To access and use the current :doc:`locale ` + +Setting and retrieving the locale can be done via configuration or using +container parameters, listeners, route parameters or the current request. + +Thanks to the ``Translation`` contract, the locale can be set via services. + +To register your own locale aware service, first create a service that implements +the :class:`Symfony\\Contracts\\Translation\\LocaleAwareInterface` interface:: + + // src/Locale/MyCustomLocaleHandler.php + namespace App\Locale; + + use Symfony\Contracts\Translation\LocaleAwareInterface; + + class MyCustomLocaleHandler implements LocaleAwareInterface + { + public function setLocale($locale) + { + $this->locale = $locale; + } + + public function getLocale() + { + return $this->locale; + } + } + +If you're using the :ref:`default services.yaml configuration `, +your service will be automatically tagged with ``kernel.locale_aware``. But, you +can also register it manually: + +.. configuration-block:: + + .. code-block:: yaml + + services: + App\Locale\MyCustomLocaleHandler: + tags: [kernel.locale_aware] + + .. code-block:: xml + + + + + + + + + + + + .. code-block:: php + + use App\Locale\MyCustomLocaleHandler; + + $container + ->register(LocaleHandler::class) + ->addTag('kernel.locale_aware') + ; + kernel.reset ------------