Skip to content

Commit 13dc21c

Browse files
committed
Use a locale-aware service to configure the translatable listener
Registering it in the LocaleSwitcher of Symfony has multiple benefits: - it allows respecting the locale when it is switched in non-request context (or after the listener that was running for request contexts), for instance in commands or in queue consumers - it avoids leaking the locale of a request to the beginning of the processing of the next one when using Frankenphp in worker mode (or as the LocaleSwitcher of Symfony resets the locale to the default locale of the project when necessary.
1 parent 4749cd9 commit 13dc21c

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
],
1414
"require": {
1515
"php": "^8.1",
16+
"gedmo/doctrine-extensions": "^3.20.0",
1617
"symfony/cache": "^6.4 || ^7.0",
1718
"symfony/config": "^6.4 || ^7.0",
1819
"symfony/dependency-injection": "^6.4 || ^7.0",
1920
"symfony/event-dispatcher": "^6.4 || ^7.0",
2021
"symfony/http-kernel": "^6.4 || ^7.0",
21-
"gedmo/doctrine-extensions": "^3.20.0"
22+
"symfony/translation-contracts": "^2.5 || ^3.5"
2223
},
2324
"require-dev": {
2425
"phpstan/phpstan": "^2.1",

src/EventListener/LocaleListener.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
use Symfony\Component\HttpKernel\KernelEvents;
99

1010
/**
11-
* This listeners sets the current locale for the TranslatableListener
11+
* This listener sets the current locale for the TranslatableListener
1212
*
1313
* @author Christophe COEVOET
14+
*
15+
* @deprecated since 1.14. Use the LocaleSynchronizer instead.
1416
*/
1517
class LocaleListener implements EventSubscriberInterface
1618
{

src/Resources/config/translatable.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,14 @@
3333
</call>
3434
</service>
3535

36+
<service id="stof_doctrine_extensions.tool.locale_synchronizer" class="Stof\DoctrineExtensionsBundle\Tool\LocaleSynchronizer" public="false">
37+
<argument type="service" id="stof_doctrine_extensions.listener.translatable" />
38+
<tag name="kernel.locale_aware" />
39+
</service>
40+
3641
<service id="stof_doctrine_extensions.event_listener.locale" class="%stof_doctrine_extensions.event_listener.locale.class%">
42+
<deprecated package="stof/doctrine-extensions-bundle" version="1.14">The "%service_id%" service is deprecated and will be removed in 2.0. The "stof_doctrine_extensions.tool.locale_synchronizer" service should be used to provide the user instead.</deprecated>
3743
<argument type="service" id="stof_doctrine_extensions.listener.translatable" />
38-
<tag name="kernel.event_subscriber" />
3944
</service>
4045
</services>
4146
</container>

src/Tool/LocaleSynchronizer.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Stof\DoctrineExtensionsBundle\Tool;
4+
5+
use Gedmo\Translatable\TranslatableListener;
6+
use Symfony\Contracts\Translation\LocaleAwareInterface;
7+
8+
/**
9+
* @internal
10+
*/
11+
final class LocaleSynchronizer implements LocaleAwareInterface
12+
{
13+
private TranslatableListener $listener;
14+
15+
public function __construct(TranslatableListener $listener)
16+
{
17+
$this->listener = $listener;
18+
}
19+
20+
public function setLocale(string $locale): void
21+
{
22+
$this->listener->setTranslatableLocale($locale);
23+
}
24+
25+
public function getLocale(): string
26+
{
27+
return $this->listener->getListenerLocale();
28+
}
29+
}

0 commit comments

Comments
 (0)