diff --git a/src/Symfony/Bridge/Twig/AppVariable.php b/src/Symfony/Bridge/Twig/AppVariable.php index abcbff9255ad5..ea178a27fcf56 100644 --- a/src/Symfony/Bridge/Twig/AppVariable.php +++ b/src/Symfony/Bridge/Twig/AppVariable.php @@ -31,6 +31,7 @@ class AppVariable private string $environment; private bool $debug; private LocaleSwitcher $localeSwitcher; + private array $enabledLocales; /** * @return void @@ -69,6 +70,11 @@ public function setLocaleSwitcher(LocaleSwitcher $localeSwitcher): void $this->localeSwitcher = $localeSwitcher; } + public function setEnabledLocales(array $enabledLocales): void + { + $this->enabledLocales = $enabledLocales; + } + /** * Returns the current token. * @@ -155,6 +161,15 @@ public function getLocale(): string return $this->localeSwitcher->getLocale(); } + public function getEnabled_locales(): array + { + if (!isset($this->enabledLocales)) { + throw new \RuntimeException('The "app.enabled_locales" variable is not available.'); + } + + return $this->enabledLocales; + } + /** * Returns some or all the existing flash messages: * * getFlashes() returns all the flash messages diff --git a/src/Symfony/Bridge/Twig/CHANGELOG.md b/src/Symfony/Bridge/Twig/CHANGELOG.md index 89f3b00a74e3a..4e8ed13101b8a 100644 --- a/src/Symfony/Bridge/Twig/CHANGELOG.md +++ b/src/Symfony/Bridge/Twig/CHANGELOG.md @@ -6,6 +6,7 @@ CHANGELOG * Allow an array to be passed as the first argument to the `importmap()` Twig function * Add `TemplatedEmail::locale()` to set the locale for the email rendering + * Add `AppVariable::getEnabledLocales()` to retrieve the enabled locales 6.3 --- diff --git a/src/Symfony/Bridge/Twig/Tests/AppVariableTest.php b/src/Symfony/Bridge/Twig/Tests/AppVariableTest.php index 6dc75cdd2ab86..d7561cc9d48bf 100644 --- a/src/Symfony/Bridge/Twig/Tests/AppVariableTest.php +++ b/src/Symfony/Bridge/Twig/Tests/AppVariableTest.php @@ -112,6 +112,13 @@ public function testGetLocale() self::assertEquals('fr', $this->appVariable->getLocale()); } + public function testGetEnabledLocales() + { + $this->appVariable->setEnabledLocales(['en', 'fr']); + + self::assertSame(['en', 'fr'], $this->appVariable->getEnabled_locales()); + } + public function testGetTokenWithNoToken() { $tokenStorage = $this->createMock(TokenStorageInterface::class); @@ -171,6 +178,13 @@ public function testGetLocaleWithLocaleSwitcherNotSet() $this->appVariable->getLocale(); } + public function testGetEnabledLocalesWithEnabledLocalesNotSet() + { + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('The "app.enabled_locales" variable is not available.'); + $this->appVariable->getEnabled_locales(); + } + public function testGetFlashesWithNoRequest() { $this->setRequestStack(null); diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.php b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.php index 6525d875a5737..556cdde3c183f 100644 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.php +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.php @@ -77,6 +77,7 @@ ->call('setTokenStorage', [service('security.token_storage')->ignoreOnInvalid()]) ->call('setRequestStack', [service('request_stack')->ignoreOnInvalid()]) ->call('setLocaleSwitcher', [service('translation.locale_switcher')->ignoreOnInvalid()]) + ->call('setEnabledLocales', [param('kernel.enabled_locales')]) ->set('twig.template_iterator', TemplateIterator::class) ->args([service('kernel'), abstract_arg('Twig paths'), param('twig.default_path'), abstract_arg('File name pattern')])