8000 [Translation][FrameworkBundle] add `LocaleSwitcher` service · symfony/symfony@627351a · GitHub
[go: up one dir, main page]

Skip to content

Commit 627351a

Browse files
committed
[Translation][FrameworkBundle] add LocaleSwitcher service
1 parent 2f27b39 commit 627351a

File tree

2 files changed

+79
-0
lines changed
10000

2 files changed

+79
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
use Symfony\Component\Translation\Loader\QtFileLoader;
4040
use Symfony\Component\Translation\Loader\XliffFileLoader;
4141
use Symfony\Component\Translation\Loader\YamlFileLoader;
42+
use Symfony\Component\Translation\LocaleSwitcher;
4243
use Symfony\Component\Translation\LoggingTranslator;
4344
use Symfony\Component\Translation\Reader\TranslationReader;
4445
use Symfony\Component\Translation\Reader\TranslationReaderInterface;
@@ -163,4 +164,16 @@
163164
->tag('container.service_subscriber', ['id' => 'translator'])
164165
->tag('kernel.cache_warmer')
165166
;
167+
168+
if (class_exists(LocaleSwitcher::class)) {
169+
$container->services()
170+
->set('translation.locale_switcher', LocaleSwitcher::class)
171+
->args([
172+
param('kernel.default_locale'),
173+
tagged_iterator('kernel.locale_aware'),
174+
service('router.request_context')->ignoreOnInvalid(),
175+
])
176+
->alias(LocaleSwitcher::class, 'translation.locale_switcher')
177+
;
178+
}
166179
};
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation;
13+
14+
use Symfony\Component\Routing\RequestContext;
15+
use Symfony\Contracts\Translation\LocaleAwareInterface;
16+
17+
/**
18+
* @author Kevin Bond <kevinbond@gmail.com>
19+
*/
20+
final class LocaleSwitcher implements LocaleAwareInterface
21+
{
22+
/**
23+
* @param LocaleAwareInterface[] $localeAwareServices
24+
*/
25+
public function __construct(
26+
private string $locale,
27+
private iterable $localeAwareServices,
28+
private ?RequestContext $requestContext = null
29+
) {
30+
}
31+
32+
public function setLocale(string $locale): void
33+
{
34+
\Locale::setDefault($locale);
35+
36+
foreach ($this->localeAwareServices as $service) {
37+
$service->setLocale($locale);
38+
}
39+
40+
if ($this->requestContext) {
41+
$this->requestContext->setParameter('_locale', $locale);
42+
}
43+
}
44+
45+
public function getLocale(): string
46+
{
47+
return $this->locale;
48+
}
49+
50+
/**
51+
* Switch to a new locale, execute a callback, then switch back to the original.
52+
*
53+
* @param callable():void $callback
54+
*/
55+
public function switchLocale(string $locale, callable $callback): void
56+
{
57+
$original = $this->getLocale();
58+
$this->setLocale($locale);
59+
60+
try {
61+
$callback();
62+
} finally {
63+
$this->setLocale($original);
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)
0