Skip to content

Commit de881b3

Browse files
committed
minor #16639 [DependencyInjection][Framework] kernel.locale_aware (Guikingone)
This PR was submitted for the 4.3 branch but it was merged into the 4.4 branch instead. Discussion ---------- [DependencyInjection][Framework] `kernel.locale_aware` Hi 👋🏻 It seems that the `kernel.locale_aware` tag was introduced in `4.3` without documenting it, here's a first draft. Thanks for the feedback 🙂 Commits ------- c1ab456 refactor(reference): document kernel.locale_aware
2 parents ebda029 + c1ab456 commit de881b3

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

reference/dic_tags.rst

+71
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,77 @@ To add a new rendering strategy - in addition to the core strategies like
453453
:class:`Symfony\\Component\\HttpKernel\\Fragment\\FragmentRendererInterface`,
454454
register it as a service, then tag it with ``kernel.fragment_renderer``.
455455

456+
kernel.locale_aware
457+
-------------------
458+
459+
.. versionadded:: 4.3
460+
461+
The ``kernel.locale_aware`` tag was introduced in Symfony 4.3.
462+
463+
**Purpose**: To access and use the current :doc:`locale </translation/locale>`
464+
465+
Setting and retrieving the locale can be done via configuration or using
466+
container parameters, listeners, route parameters or the current request.
467+
468+
Thanks to the ``Translation`` contract, the locale can be set via services.
469+
470+
To register your own locale aware service, first create a service that implements
471+
the :class:`Symfony\\Contracts\\Translation\\LocaleAwareInterface` interface::
472+
473+
// src/Locale/MyCustomLocaleHandler.php
474+
namespace App\Locale;
475+
476+
use Symfony\Contracts\Translation\LocaleAwareInterface;
477+
478+
class MyCustomLocaleHandler implements LocaleAwareInterface
479+
{
480+
public function setLocale($locale)
481+
{
482+
$this->locale = $locale;
483+
}
484+
485+
public function getLocale()
486+
{
487+
return $this->locale;
488+
}
489+
}
490+
491+
If you're using the :ref:`default services.yaml configuration <service-container-services-load-example>`,
492+
your service will be automatically tagged with ``kernel.locale_aware``. But, you
493+
can also register it manually:
494+
495+
.. configuration-block::
496+
497+
.. code-block:: yaml
498+
499+
services:
500+
App\Locale\MyCustomLocaleHandler:
501+
tags: [kernel.locale_aware]
502+
503+
.. code-block:: xml
504+
505+
<?xml version="1.0" encoding="UTF-8" ?>
506+
<container xmlns="http://symfony.com/schema/dic/services"
507+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
508+
xsi:schemaLocation="http://symfony.com/schema/dic/services
509+
https://symfony.com/schema/dic/services/services-1.0.xsd">
510+
511+
<services>
512+
<service id="App\Locale\MyCustomLocaleHandler">
513+
<tag name="kernel.locale_aware"/>
514+
</service>
515+
</services>
516+
</container>
517+
518+
.. code-block:: php
519+
520+
use App\Locale\MyCustomLocaleHandler;
521+
522+
$container
523+
->register(LocaleHandler::class)
524+
->addTag('kernel.locale_aware')
525+
;
526+
456527
kernel.reset
457528
------------
458529

0 commit comments

Comments
 (0)