Skip to content

Commit 73a3f78

Browse files
committed
feature symfony#50321 [TwigBridge] Add AppVariable::getEnabledLocales() (jmsche)
This PR was merged into the 6.4 branch. Discussion ---------- [TwigBridge] Add `AppVariable::getEnabledLocales()` | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | N/A | License | MIT | Doc PR | symfony/symfony-docs#... This PR aims to provide a new `app.enabled_locales` getter for templates to retrieve the enabled locales configured using the `framework.enabled_locales` configuration, to make e.g. a locale switcher in the templates without to have to pass the enabled locales using e.g. a Twig global. The docs PR will me made if the PR is accepted. Targeting the 6.3 branch for now, will switch to 6.4 when it will be available (as feature freeze is active since late March). Commits ------- 3e1925c [TwigBridge] Add `AppVariable::getEnabledLocales()` to retrieve the enabled locales
2 parents 9697dc6 + 3e1925c commit 73a3f78

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-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: 1 addition & 0 deletions
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

Lines changed: 14 additions & 0 deletions
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

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)