Skip to content

Commit 6ac6fae

Browse files
authored
refactor(reference): document kernel.locale_aware
1 parent 96723ac commit 6ac6fae

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

reference/dic_tags.rst

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Tag Name Usage
2626
`kernel.event_listener`_ Listen to different events/hooks in Symfony
2727
`kernel.event_subscriber`_ To subscribe to a set of different events/hooks in Symfony
2828
`kernel.fragment_renderer`_ Add new HTTP content rendering strategies
29+
`kernel.locale_aware`_ To interact with the locale
2930
`kernel.reset`_ Allows to clean up services between requests
3031
`mime.mime_type_guesser`_ Add your own logic for guessing MIME types
3132
`monolog.logger`_ Logging with a custom logging channel
@@ -470,6 +471,77 @@ To add a new rendering strategy - in addition to the core strategies like
470471
:class:`Symfony\\Component\\HttpKernel\\Fragment\\FragmentRendererInterface`,
471472
register it as a service, then tag it with ``kernel.fragment_renderer``.
472473

474+
kernel.locale_aware
475+
-------------------
476+
477+
.. versionadded:: 4.3
478+
479+
The ``kernel.locale_aware`` tag was introduced in Symfony 4.3.
480+
481+
**Purpose**: To interact with the locale
482+
483+
Setting and retrieving the locale can be done via the configuration or
484+
thanks to container parameters or even listeners / route parameters.
485+
486+
Thanks to the ``Translation`` contract, the locale can be set via services.
487+
488+
To register your own locale aware service, first create a service that implements
489+
the :class:`Symfony\\Contracts\\Translation\\LocaleAwareInterface` interface::
490+
491+
// src/Locale/MyCustomLocaleHandler.php
492+
namespace App\Locale;
493+
494+
use Symfony\Contracts\Translation\LocaleAwareInterface;
495+
496+
class MyCustomLocaleHandler implements LocaleAwareInterface
497+
{
498+
public function setLocale($locale)
499+
{
500+
// ... do some sort of operations to set the locale
501+
}
502+
503+
public function getLocale()
504+
{
505+
return 'en';
506+
}
507+
}
508+
509+
If you're using the :ref:`default services.yaml configuration <service-container-services-load-example>`,
510+
your service will be automatically tagged with ``kernel.locale_aware``. But, you
511+
can also register it manually:
512+
513+
.. configuration-block::
514+
515+
.. code-block:: yaml
516+
517+
services:
518+
App\Locale\MyCustomLocaleHandler:
519+
tags: [kernel.locale_aware]
520+
521+
.. code-block:: xml
522+
523+
<?xml version="1.0" encoding="UTF-8" ?>
524+
<container xmlns="http://symfony.com/schema/dic/services"
525+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
526+
xsi:schemaLocation="http://symfony.com/schema/dic/services
527+
https://symfony.com/schema/dic/services/services-1.0.xsd">
528+
529+
<services>
530+
<service id="App\Locale\MyCustomLocaleHandler">
531+
<tag name="kernel.locale_aware"/>
532+
</service>
533+
</services>
534+
</container>
535+
536+
.. code-block:: php
537+
538+
use App\Locale\MyCustomLocaleHandler;
539+
540+
$container
541+
->register(LocaleHandler::class)
542+
->addTag('kernel.locale_aware')
543+
;
544+
473545
kernel.reset
474546
------------
475547

0 commit comments

Comments
 (0)