8000 [TwigBridge] Add `AppVariable::getEnabledLocales()` to retrieve the e… · symfony/symfony@3e1925c · GitHub
[go: up one dir, main page]

Skip to content

Commit 3e1925c

Browse files
jmschefabpot
authored andcommitted
[TwigBridge] Add AppVariable::getEnabledLocales() to retrieve the enabled locales
1 parent 9697dc6 commit 3e1925c

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

src/Symfony/Bridge/Twig/AppVariable.php

+15
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

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Allow an array to be passed as the first argument to the `importmap()` Twig function
88
* Add `TemplatedEmail::locale()` to set the locale for the email rendering
9+
* Add `AppVariable::getEnabledLocales()` to retrieve the enabled locales
910

1011
6.3
1112
---

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

+14
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ public function testGetLocale()
112112
self::assertEquals('fr', $this->appVariable->getLocale());
113113
}
114114

115+
public function testGetEnabledLocales()
116+
{
117+
$this->appVariable->setEnabledLocales(['en', 'fr']);
118+
119+
self::assertSame(['en', 'fr'], $this->appVariable->getEnabled_locales());
120+
}
121+
115122
public function testGetTokenWithNoToken()
116123
{
117124
$tokenStorage = $this->createMock(TokenStorageInterface::class);
@@ -171,6 +178,13 @@ public function testGetLocaleWithLocaleSwitcherNotSet()
171178
$this->appVariable->getLocale();
172179
}
173180

181+
public function testGetEnabledLocalesWithEnabledLocalesNotSet()
182+
{
183+
$this->expectException(\RuntimeException::class);
184+
$this->expectExceptionMessage('The "app.enabled_locales" variable is not available.');
185+
$this->appVariable->getEnabled_locales();
186+
}
187+
174188
public function testGetFlashesWithNoRequest()
175189
{
176190
$this->setRequestStack(null);

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

+1
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)
0