8000 Set the default locale early · symfony/symfony@02c9f35 · GitHub
[go: up one dir, main page]

Skip to content

Commit 02c9f35

Browse files
Set the default locale early
1 parent d12a6d0 commit 02c9f35

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

src/Symfony/Component/HttpKernel/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.3.0
5+
-----
6+
7+
* made `Symfony\Component\HttpKernel\EventListenerLocaleListener` set the default locale early
8+
49
4.2.0
510
-----
611

src/Symfony/Component/HttpKernel/EventListener/LocaleListener.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\HttpFoundation\RequestStack;
1717
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
1818
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
19+
use Symfony\Component\HttpKernel\Event\KernelEvent;
1920
use Symfony\Component\HttpKernel\KernelEvents;
2021
use Symfony\Component\Routing\RequestContextAwareInterface;
2122

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

46+
public function setDefaultLocale(KernelEvent $event)
47+
{
48+
$event->getRequest()->setDefaultLocale($this->defaultLocale);
49+
}
50+
4551
public function onKernelRequest(GetResponseEvent $event)
4652
{
4753
$request = $event->getRequest();
48-
$request->setDefaultLocale($this->defaultLocale);
4954

5055
$this->setLocale($request);
5156
$this->setRouterContext($request);
@@ -75,8 +80,11 @@ private function setRouterContext(Request $request)
7580
public static function getSubscribedEvents()
7681
{
7782
return array(
78-
// must be registered after the Router to have access to the _locale
79-
KernelEvents::REQUEST => array(array('onKernelRequest', 16)),
83+
KernelEvents::REQUEST => array(
84+
array('setDefaultLocale', 100),
85+
// must be registered after the Router to have access to the _locale
86+
array('onKernelRequest', 16),
87+
),
8088
KernelEvents::FINISH_REQUEST => array(array('onKernelFinishRequest', 0)),
8189
);
8290
}

src/Symfony/Component/HttpKernel/Tests/EventListener/LocaleListenerTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
namespace Symfony\Component\HttpKernel\Tests\EventListener;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1516
use Symfony\Component\HttpFoundation\Request;
1617
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
1718
use Symfony\Component\HttpKernel\EventListener\LocaleListener;
1819
use Symfony\Component\HttpKernel\HttpKernelInterface;
20+
use Symfony\Component\HttpKernel\KernelEvents;
1921

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

29-
public function testDefaultLocaleWithoutSession()
31+
public function testIsAnEventSubscriber()
32+
{
33+
$this->assertInstanceOf(EventSubscriberInterface::class, new LocaleListener($this->requestStack));
34+
}
35+
36+
public function testRegisteredEvent()
37+
{
38+
$this->assertEquals(
39+
array(
40+
KernelEvents::REQUEST => array(array('setDefaultLocale', 100), array('onKernelRequest', 16)),
41+
KernelEvents::FINISH_REQUEST => array(array('onKernelFinishRequest', 0)),
42+
),
43+
LocaleListener::getSubscribedEvents()
44+
);
45+
}
46 6293 +
47+
public function testDefaultLocale()
3048
{
3149
$listener = new LocaleListener($this->requestStack, 'fr');
3250
$event = $this->getEvent($request = Request::create('/'));
3351

34-
$listener->onKernelRequest($event);
52+
$listener->setDefaultLocale($event);
3553
$this->assertEquals('fr', $request->getLocale());
3654
}
3755

0 commit comments

Comments
 (0)
0