Skip to content

Commit f4f39d2

Browse files
committed
[TwigBridge] Add AppVariable::getEnabledLocales() to retrieve the enabled locales
1 parent eb3e308 commit f4f39d2

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

src/Symfony/Bridge/Twig/AppVariable.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class AppVariable
3131
private string $environment;
3232
private bool $debug;
3333
private LocaleSwitcher $localeSwitcher;
34+
private array $enabledLocales;
3435

3536
/**
3637
* @return void
@@ -69,6 +70,11 @@ public function setLocaleSwitcher(LocaleSwitcher $localeSwitcher): void
6970
$this->localeSwitcher = $localeSwitcher;
7071
}
7172

73+
public function setEnabledLocales(array $enabledLocales): void
74+
{
75+
$this->enabledLocales = $enabledLocales;
76+
}
77+
7278
/**
7379
* Returns the current token.
7480
*
@@ -155,6 +161,15 @@ public function getLocale(): string
155161
return $this->localeSwitcher->getLocale();
156162
}
157163

164+
public function getEnabled_locales(): array
165+
{
166+
if (!isset($this->enabledLocales)) {
167+
throw new \RuntimeException('The "app.enabled_locales" variable is not available.');
168+
}
169+
170+
return $this->enabledLocales;
171+
}
172+
158173
/**
159174
* Returns some or all the existing flash messages:
160175
* * getFlashes() returns all the flash messages

src/Symfony/Bridge/Twig/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.4
5+
---
6+
7+
* Add `AppVariable::getEnabledLocales()` to retrieve the enabled locales
8+
49
6.3
510
---
611

src/Symfony/Bridge/Twig/Tests/AppVariableTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ public function testGetLocale()
115115
self::assertEquals('fr', $this->appVariable->getLocale());
116116
}
117117

118+
public function testGetEnabledLocales()
119+
{
120+
$this->appVariable->setEnabledLocales(['en', 'fr']);
121+
122+
self::assertSame(['en', 'fr'], $this->appVariable->getEnabled_locales());
123+
}
124+
118125
public function testGetTokenWithNoToken()
119126
{
120127
$tokenStorage = $this->createMock(TokenStorageInterface::class);
@@ -174,6 +181,13 @@ public function testGetLocaleWithLocaleSwitcherNotSet()
174181
$this->appVariable->getLocale();
175182
}
176183

184+
public function testGetEnabledLocalesWithEnabledLocalesNotSet()
185+
{
186+
$this->expectException(\RuntimeException::class);
187+
$this->expectExceptionMessage('The "app.enabled_locales" variable is not available.');
188+
$this->appVariable->getEnabled_locales();
189+
}
190+
177191
public function testGetFlashesWithNoRequest()
178192
{
179193
$this->setRequestStack(null);

src/Symfony/Bundle/TwigBundle/Resources/config/twig.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
->call('setTokenStorage', [service('security.token_storage')->ignoreOnInvalid()])
7878
->call('setRequestStack', [service('request_stack')->ignoreOnInvalid()])
7979
->call('setLocaleSwitcher', [service('translation.locale_switcher')->ignoreOnInvalid()])
80+
->call('setEnabledLocales', [param('kernel.enabled_locales')])
8081

8182
->set('twig.template_iterator', TemplateIterator::class)
8283
->args([service('kernel'), abstract_arg('Twig paths'), param('twig.default_path'), abstract_arg('File name pattern')])

0 commit comments

Comments
 (0)