Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[TwigBridge][TwigBundle] Add current locale to AppVariable
  • Loading branch information
SVillette authored and fabpot committed Apr 11, 2023
commit 2371216d9349565f564a6fd8daecf323309f1e60
16 changes: 16 additions & 0 deletions src/Symfony/Bridge/Twig/AppVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Translation\LocaleSwitcher;

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

/**
* @return void
Expand Down Expand Up @@ -62,6 +64,11 @@ public function setDebug(bool $debug)
$this->debug = $debug;
}

public function setLocaleSwitcher(LocaleSwitcher $localeSwitcher): void
{
$this->localeSwitcher = $localeSwitcher;
}

/**
* Returns the current token.
*
Expand Down Expand Up @@ -139,6 +146,15 @@ public function getDebug(): bool
return $this->debug;
}

public function getLocale(): string
{
if (!isset($this->localeSwitcher)) {
throw new \RuntimeException('The "app.locale" variable is not available.');
}

return $this->localeSwitcher->getLocale();
}

/**
* Returns some or all the existing flash messages:
* * getFlashes() returns all the flash messages
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Bridge/Twig/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.3
---

* Add `AppVariable::getLocale()` to retrieve the current locale when using the `LocaleSwitcher`

6.2
---

Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Bridge/Twig/Tests/AppVariableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Translation\LocaleSwitcher;

class AppVariableTest extends TestCase
{
Expand Down Expand Up @@ -104,6 +105,16 @@ public function testGetUser()
$this->assertEquals($user, $this->appVariable->getUser());
}

public function testGetLocale()
{
$localeSwitcher = $this->createMock(LocaleSwitcher::class);
$this->appVariable->setLocaleSwitcher($localeSwitcher);

$localeSwitcher->method('getLocale')->willReturn('fr');

self::assertEquals('fr', $this->appVariable->getLocale());
}

public function testGetTokenWithNoToken()
{
$tokenStorage = $this->createMock(TokenStorageInterface::class);
Expand Down Expand Up @@ -156,6 +167,13 @@ public function testGetSessionWithRequestStackNotSet()
$this->appVariable->getSession();
}

public function testGetLocaleWithLocaleSwitcherNotSet()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('The "app.locale" variable is not available.');
$this->appVariable->getLocale();
}

public function testGetFlashesWithNoRequest()
{
$this->setRequestStack(null);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Twig/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"symfony/polyfill-intl-icu": "~1.0",
"symfony/property-info": "^5.4|^6.0",
"symfony/routing": "^5.4|^6.0",
"symfony/translation": "^5.4|^6.0",
"symfony/translation": "^6.1",
"symfony/yaml": "^5.4|^6.0",
"symfony/security-acl": "^2.8|^3.0",
"symfony/security-core": "^5.4|^6.0",
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
->call('setDebug', [param('kernel.debug')])
->call('setTokenStorage', [service('security.token_storage')->ignoreOnInvalid()])
->call('setRequestStack', [service('request_stack')->ignoreOnInvalid()])
->call('setLocaleSwitcher', [service('translation.locale_switcher')->ignoreOnInvalid()])

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