Skip to content

Commit 94e782e

Browse files
[Translation] Improve LocaleSwitcher a bit
1 parent 0a31f60 commit 94e782e

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

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 <[email protected]>
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
}

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

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"symfony/http-kernel": "^5.4|^6.0",
2929
"symfony/intl": "^5.4|^6.0",
3030
"symfony/polyfill-intl-icu": "^1.21",
31+
"symfony/routing": "^5.4|^6.0",
3132
"symfony/service-contracts": "^1.1.2|^2|^3",
3233
"symfony/yaml": "^5.4|^6.0",
3334
"symfony/finder": "^5.4|^6.0",

0 commit comments

Comments
 (0)