8000 [HttpKernel] Set the default locale early by thewilkybarkid · Pull Request #29483 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpKernel] Set the default lo 8000 cale early #29483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 2, 2019
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.3.0
-----

* made `Symfony\Component\HttpKernel\EventListenerLocaleListener` set the default locale early

4.2.0
-----

Expand Down
14 changes: 11 additions & 3 deletions src/Symfony/Component/HttpKernel/EventListener/LocaleListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\KernelEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\RequestContextAwareInterface;

Expand All @@ -42,10 +43,14 @@ public function __construct(RequestStack $requestStack, string $defaultLocale =
$this->router = $router;
}

public function setDefaultLocale(KernelEvent $event)
{
$event->getRequest()->setDefaultLocale($this->defaultLocale);
}

public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
$request->setDefaultLocale($this->defaultLocale);

$this->setLocale($request);
$this->setRouterContext($request);
Expand Down Expand Up @@ -75,8 +80,11 @@ private function setRouterContext(Request $request)
public static function getSubscribedEvents()
{
return array(
// must be registered after the Router to have access to the _locale
KernelEvents::REQUEST => array(array('onKernelRequest', 16)),
KernelEvents::REQUEST => array(
array('setDefaultLocale', 100),
// must be registered after the Router to have access to the _locale
array('onKernelRequest', 16),
),
KernelEvents::FINISH_REQUEST => array(array('onKernelFinishRequest', 0)),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
namespace Symfony\Component\HttpKernel\Tests\EventListener;

use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\EventListener\LocaleListener;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;

class LocaleListenerTest extends TestCase
{
Expand All @@ -26,12 +28,28 @@ protected function setUp()
$this->requestStack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->disableOriginalConstructor()->getMock();
}

public function testDefaultLocaleWithoutSession()
public function testIsAnEventSubscriber()
{
$this->assertInstanceOf(EventSubscriberInterface::class, new LocaleListener($this->requestStack));
}

public function testRegisteredEvent()
{
$this->assertEquals(
array(
KernelEvents::REQUEST => array(array('setDefaultLocale', 100), array('onKernelRequest', 16)),
KernelEvents::FINISH_REQUEST => array(array('onKernelFinishRequest', 0)),
),
LocaleListener::getSubscribedEvents()
);
}

public function testDefaultLocale()
{
$listener = new LocaleListener($this->requestStack, 'fr');
$event = $this->getEvent($request = Request::create('/'));

$listener->onKernelRequest($event);
$listener->setDefaultLocale($event);
$this->assertEquals('fr', $request->getLocale());
}

Expand Down
0