Set the default locale early
This commit is contained in:
parent
d12a6d0330
commit
02c9f352d9
@ -1,6 +1,11 @@
|
||||
CHANGELOG
|
||||
=========
|
||||
|
||||
4.3.0
|
||||
-----
|
||||
|
||||
* made `Symfony\Component\HttpKernel\EventListenerLocaleListener` set the default locale early
|
||||
|
||||
4.2.0
|
||||
-----
|
||||
|
||||
|
@ -16,6 +16,7 @@ use Symfony\Component\HttpFoundation\Request;
|
||||
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;
|
||||
|
||||
@ -42,10 +43,14 @@ class LocaleListener implements EventSubscriberInterface
|
||||
$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);
|
||||
@ -75,8 +80,11 @@ class LocaleListener implements EventSubscriberInterface
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return array(
|
||||
KernelEvents::REQUEST => array(
|
||||
array('setDefaultLocale', 100),
|
||||
// must be registered after the Router to have access to the _locale
|
||||
KernelEvents::REQUEST => array(array('onKernelRequest', 16)),
|
||||
array('onKernelRequest', 16),
|
||||
),
|
||||
KernelEvents::FINISH_REQUEST => array(array('onKernelFinishRequest', 0)),
|
||||
);
|
||||
}
|
||||
|
@ -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
|
||||
{
|
||||
@ -26,12 +28,28 @@ class LocaleListenerTest extends TestCase
|
||||
$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());
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user