Skip to content

Commit e270317

Browse files
[Translation] Improve LocaleSwitcher a bit
1 parent f1ce841 commit e270317

File tree

11 files changed

+56
-204
lines changed

11 files changed

+56
-204
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/ConfigureLocaleSwitcherPass.php

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@
205205
use Symfony\Component\Translation\Bridge\Loco\LocoProviderFactory;
206206
use Symfony\Component\Translation\Bridge\Lokalise\LokaliseProviderFactory;
207207
use Symfony\Component\Translation\Command\XliffLintCommand as BaseXliffLintCommand;
208+
use Symfony\Component\Translation\LocaleSwitcher;
208209
use Symfony\Component\Translation\PseudoLocalizationTranslator;
209210
use Symfony\Component\Translation\Translator;
210211
use Symfony\Component\Uid\Factory\UuidFactory;
@@ -1082,7 +1083,6 @@ private function registerRouterConfiguration(array $config, ContainerBuilder $co
10821083
$container->removeDefinition('console.command.router_debug');
10831084
$container->removeDefinition('console.command.router_match');
10841085
$container->removeDefinition('messenger.middleware.router_context');
1085-
$container->removeDefinition('translation.locale_aware_request_context');
10861086

10871087
return;
10881088
}
@@ -1295,6 +1295,11 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
12951295
}
12961296

12971297
$loader->load('translation.php');
1298+
1299+
if (!ContainerBuilder::willBeAvailable('symfony/translation', LocaleSwitcher::class, ['symfony/framework-bundle'])) {
1300+
$container->removeDefinition('translation.locale_switcher');
1301+
}
1302+
12981303
$loader->load('translation_providers.php');
12991304

13001305
// Use the "real" translator instead of the identity default

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ public function build(ContainerBuilder $container)
164164
$container->addCompilerPass(new RegisterReverseContainerPass(true));
165165
$container->addCompilerPass(new RegisterReverseContainerPass(false), PassConfig::TYPE_AFTER_REMOVING);
166166
$container->addCompilerPass(new RemoveUnusedSessionMarshallingHandlerPass());
167-
$container->addCompilerPass(new ConfigureLocaleSwitcherPass());
168167

169168
if ($container->getParameter('kernel.debug')) {
170169
$container->addCompilerPass(new AddDebugLogProcessorPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 2);

src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Psr\Container\ContainerInterface;
1515
use Symfony\Bundle\FrameworkBundle\CacheWarmer\TranslationsCacheWarmer;
16-
use Symfony\Bundle\FrameworkBundle\Translation\LocaleAwareRequestContext;
1716
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
1817
use Symfony\Component\Translation\Dumper\CsvFileDumper;
1918
use Symfony\Component\Translation\Dumper\IcuResFileDumper;
@@ -164,22 +163,13 @@
164163
->args([service(ContainerInterface::class)])
165164
->tag('container.service_subscriber', ['id' => 'translator'])
166165
->tag('kernel.cache_warmer')
167-
;
168-
169-
if (class_exists(LocaleSwitcher::class)) {
170-
$container->services()
171-
->set('translation.locale_switcher', LocaleSwitcher::class)
172-
->args([
173-
param('kernel.default_locale'),
174-
abstract_arg('LocaleAware services'),
175-
])
176-
->alias(LocaleSwitcher::class, 'translation.locale_switcher')
177166

178-
->set('translation.locale_aware_request_context', LocaleAwareRequestContext::class)
167+
->set('translation.locale_switcher', LocaleSwitcher::class)
179168
->args([
180-
service('router.request_context'),
181169
param('kernel.default_locale'),
170+
tagged_iterator('kernel.locale_aware'),
182171
])
183-
;
184-
}
172+
->tag('kernel.reset', ['method' => 'reset'])
173+
->alias(LocaleSwitcher::class, 'translation.locale_switcher')
174+
;
185175
};

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/ConfigureLocaleSwitcherPassTest.php

Lines changed: 0 additions & 62 deletions
This file was deleted.

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
3232
use Symfony\Component\Cache\DependencyInjection\CachePoolPass;
3333
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
34-
use Symfony\Component\DependencyInjection\Argument\AbstractArgument;
34+
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
3535
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
3636
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
3737
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
@@ -2019,15 +2019,11 @@ public function testLocaleSwitcherServiceRegistered()
20192019
$container = $this->createContainerFromFile('full');
20202020

20212021
$this->assertTrue($container->has('translation.locale_switcher'));
2022-
$this->assertTrue($container->has('translation.locale_aware_request_context'));
20232022

20242023
$switcherDef = $container->getDefinition('translation.locale_switcher');
2025-
$localeAwareRequestContextDef = $container->getDefinition('translation.locale_aware_request_context');
20262024

20272025
$this->assertSame('%kernel.default_locale%', $switcherDef->getArgument(0));
2028-
$this->assertInstanceOf(AbstractArgument::class, $switcherDef->getArgument(1));
2029-
$this->assertEquals(new Reference('router.request_context'), $localeAwareRequestContextDef->getArgument(0));
2030-
$this->assertSame('%kernel.default_locale%', $localeAwareRequestContextDef->getArgument(1));
2026+
$this->assertInstanceOf(IteratorArgument::class, $switcherDef->getArgument(1));
20312027
}
20322028

20332029
protected function createContainer(array $data = [])

src/Symfony/Bundle/FrameworkBundle/Tests/Translation/LocaleAwareRequestContextTest.php

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/Symfony/Bundle/FrameworkBundle/Translation/LocaleAwareRequestContext.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/Symfony/Component/Translation/LocaleSwitcher.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,31 @@
1111

1212
namespace Symfony\Component\Translation;
1313

14+
use Symfony\Component\Routing\RequestContext;
1415
use Symfony\Contracts\Translation\LocaleAwareInterface;
1516

1617
/**
1718
* @author Kevin Bond <kevinbond@gmail.com>
1819
*/
19-
final class LocaleSwitcher implements LocaleAwareInterface
20+
class LocaleSwitcher implements LocaleAwareInterface
2021
{
22+
private string $defaultLocale;
23+
2124
/**
2225
* @param LocaleAwareInterface[] $localeAwareServices
2326
*/
24-
public function __construct(private string $locale, private iterable $localeAwareServices)
25-
{
27+
public function __construct(
28+
private string $locale,
29+
private iterable $localeAwareServices,
30+
private ?RequestContext $requestContext = null,
31+
) {
32+
$this->defaultLocale = $locale;
2633
}
2734

2835
public function setLocale(string $locale): void
2936
{
3037
\Locale::setDefault($this->locale = $locale);
38+
$this->requestContext?->setParameter('_locale', $locale);
3139

3240
foreach ($this->localeAwareServices as $service) {
3341
$service->setLocale($locale);
@@ -42,17 +50,26 @@ public function getLocale(): string
4250
/**
4351
* Switch to a new locale, execute a callback, then switch back to the original.
4452
*
45-
* @param callable():void $callback
53+
* @template T
54+
*
55+
* @param callable():T $callback
56+
*
57+
* @return T
4658
*/
47-
public function runWithLocale(string $locale, callable $callback): void
59+
public function runWithLocale(string $locale, callable $callback): mixed
4860
{
4961
$original = $this->getLocale();
5062
$this->setLocale($locale);
5163

5264
try {
53-
$callback();
65+
return $callback();
5466
} finally {
5567
$this->setLocale($original);
5668
}
5769
}
70+
71+
public function reset(): void
72+
{
73+
$this->setLocale($this->defaultLocale);
74+
}
5875
}

src/Symfony/Component/Translation/Tests/LocaleSwitcherTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Translation\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Routing\RequestContext;
1516
use Symfony\Component\Translation\LocaleSwitcher;
1617
use Symfony\Contracts\Translation\LocaleAwareInterface;
1718

@@ -75,6 +76,24 @@ public function testCanSwitchLocaleForCallback()
7576
$this->assertSame('en', $service->getLocale());
7677
$this->assertSame('en', $switcher->getLocale());
7778
}
79+
80+
public function testWithRequestContext()
81+
{
82+
$context = new RequestContext();
83+
$service = new LocaleSwitcher('en', [], $context);
84+
85+
$this->assertSame('en', $service->getLocale());
86+
87+
$service->setLocale('fr');
88+
89+
$this->assertSame('fr', $service->getLocale());
90+
$this->assertSame('fr', $context->getParameter('_locale'));
91+
92+
$service->reset();
93+
94+
$this->assertSame('en', $service->getLocale());
95+
$this->assertSame('en', $context->getParameter('_locale'));
96+
}
7897
}
7998

8099
class DummyLocaleAware implements LocaleAwareInterface

0 commit comments

Comments
 (0)