Skip to content

Commit 46d28dc

Browse files
committed
feature #49913 [TwigBridge][TwigBundle] Add current locale to AppVariable (SVillette)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [TwigBridge][TwigBundle] Add current locale to `AppVariable` | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #49870 | License | MIT | Doc PR | symfony/symfony-docs#18190 As stated in #49870, they were no way to get the current locale without passing it through a variable when rendering a template within `LocaleSwitcher::runWithLocale()`. ```php #[AsController] final class HomeController { #[Route('/', name: 'app_home')] public function __invoke(LocaleSwitcher $localeSwitcher, Environment $twig): Response { $localeSwitcher->setLocale('en'); return $localeSwitcher->runWithLocale('fr', function () use ($twig) { return new Response($twig->render('index.html.twig')); }); } } ``` ```twig {{ app.locale }} // fr ``` A doc PR will be submitted if this change is accepted. Commits ------- 2371216d93 [TwigBridge][TwigBundle] Add current locale to `AppVariable`
2 parents 9ca0cd1 + 7f1d798 commit 46d28dc

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

AppVariable.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
1818
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1919
use Symfony\Component\Security\Core\User\UserInterface;
20+
use Symfony\Component\Translation\LocaleSwitcher;
2021

2122
/**
2223
* Exposes some Symfony parameters and services as an "app" global variable.
@@ -29,6 +30,7 @@ class AppVariable
2930
private RequestStack $requestStack;
3031
private string $environment;
3132
private bool $debug;
33+
private LocaleSwitcher $localeSwitcher;
3234

3335
/**
3436
* @return void
@@ -62,6 +64,11 @@ public function setDebug(bool $debug)
6264
$this->debug = $debug;
6365
}
6466

67+
public function setLocaleSwitcher(LocaleSwitcher $localeSwitcher): void
68+
{
69+
$this->localeSwitcher = $localeSwitcher;
70+
}
71+
6572
/**
6673
* Returns the current token.
6774
*
@@ -139,6 +146,15 @@ public function getDebug(): bool
139146
return $this->debug;
140147
}
141148

149+
public function getLocale(): string
150+
{
151+
if (!isset($this->localeSwitcher)) {
152+
throw new \RuntimeException('The "app.locale" variable is not available.');
153+
}
154+
155+
return $this->localeSwitcher->getLocale();
156+
}
157+
142158
/**
143159
* Returns some or all the existing flash messages:
144160
* * getFlashes() returns all the flash messages

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.3
5+
---
6+
7+
* Add `AppVariable::getLocale()` to retrieve the current locale when using the `LocaleSwitcher`
8+
49
6.2
510
---
611

Tests/AppVariableTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
2121
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
2222
use Symfony\Component\Security\Core\User\UserInterface;
23+
use Symfony\Component\Translation\LocaleSwitcher;
2324

2425
class AppVariableTest extends TestCase
2526
{
@@ -104,6 +105,16 @@ public function testGetUser()
104105
$this->assertEquals($user, $this->appVariable->getUser());
105106
}
106107

108+
public function testGetLocale()
109+
{
110+
$localeSwitcher = $this->createMock(LocaleSwitcher::class);
111+
$this->appVariable->setLocaleSwitcher($localeSwitcher);
112+
113+
$localeSwitcher->method('getLocale')->willReturn('fr');
114+
115+
self::assertEquals('fr', $this->appVariable->getLocale());
116+
}
117+
107118
public function testGetTokenWithNoToken()
108119
{
109120
$tokenStorage = $this->createMock(TokenStorageInterface::class);
@@ -156,6 +167,13 @@ public function testGetSessionWithRequestStackNotSet()
156167
$this->appVariable->getSession();
157168
}
158169

170+
public function testGetLocaleWithLocaleSwitcherNotSet()
171+
{
172+
$this->expectException(\RuntimeException::class);
173+
$this->expectExceptionMessage('The "app.locale" variable is not available.');
174+
$this->appVariable->getLocale();
175+
}
176+
159177
public function testGetFlashesWithNoRequest()
160178
{
161179
$this->setRequestStack(null);

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"symfony/polyfill-intl-icu": "~1.0",
3838
"symfony/property-info": "^5.4|^6.0",
3939
"symfony/routing": "^5.4|^6.0",
40-
"symfony/translation": "^5.4|^6.0",
40+
"symfony/translation": "^6.1",
4141
"symfony/yaml": "^5.4|^6.0",
4242
"symfony/security-acl": "^2.8|^3.0",
4343
"symfony/security-core": "^5.4|^6.0",

0 commit comments

Comments
 (0)