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

Skip to content

Commit 1b98ad3

Browse files
committed
fixed Request management for LocaleListener
1 parent a7b2b7e commit 1b98ad3

File tree

2 files changed

+13
-21
lines changed

2 files changed

+13
-21
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/web.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<tag name="kernel.event_subscriber" />
3939
<argument>%kernel.default_locale%</argument>
4040
<argument type="service" id="router" on-invalid="ignore" />
41+
<call method="setRequest"><argument type="service" id="request" on-invalid="null" strict="false" /></call>
4142
</service>
4243
</services>
4344
</container>

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

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Component\HttpKernel\EventListener;
1313

1414
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
15-
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
1615
use Symfony\Component\HttpKernel\KernelEvents;
1716
use Symfony\Component\HttpFoundation\Request;
1817
use Symfony\Component\Routing\RequestContextAwareInterface;
@@ -27,49 +26,41 @@ class LocaleListener implements EventSubscriberInterface
2726
{
2827
private $router;
2928
private $defaultLocale;
30-
private $locales = array();
3129

3230
public function __construct($defaultLocale = 'en', RequestContextAwareInterface $router = null)
3331
{
3432
$this->defaultLocale = $defaultLocale;
3533
$this->router = $router;
3634
}
3735

38-
public function onKernelResponse(FilterResponseEvent $event)
36+
public function setRequest(Request $request = null)
3937
{
40-
array_shift($this->locales);
38+
if (null === $request) {
39+
return;
40+
}
4141

42-
// setting back the locale to the previous value
43-
$locale = isset($this->locales[0]) ? $this->locales[0] : $this->defaultLocale;
44-
$request = $event->getRequest();
45-
$this->setLocale($request, $locale);
42+
if ($locale = $request->attributes->get('_locale')) {
43+
$request->setLocale($locale);
44+
}
45+
46+
if (null !== $this->router) {
47+
$this->router->getContext()->setParameter('_locale', $request->getLocale());
48+
}
4649
}
4750

4851
public function onKernelRequest(GetResponseEvent $event)
4952
{
5053
$request = $event->getRequest();
51-
5254
$request->setDefaultLocale($this->defaultLocale);
53-
$this->setLocale($request, $request->attributes->get('_locale', $this->defaultLocale));
5455

55-
array_unshift($this->locales, $request->getLocale());
56+
$this->setRequest($request);
5657
}
5758

5859
public static function getSubscribedEvents()
5960
{
6061
return array(
6162
// must be registered after the Router to have access to the _locale
6263
KernelEvents::REQUEST => array(array('onKernelRequest', 16)),
63-
KernelEvents::RESPONSE => 'onKernelResponse',
6464
);
6565
}
66-
67-
private function setLocale(Request $request, $locale)
68-
{
69-
$request->setLocale($locale);
70-
71-
if (null !== $this->router) {
72-
$this->router->getContext()->setParameter('_locale', $request->getLocale());
73-
}
74-
}
7566
}

0 commit comments

Comments
 (0)