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

Skip to content

Commit 50c3de3

Browse files
committed
merged branch fabpot/locale-fix (PR #7099)
This PR was merged into the 2.1 branch. Commits ------- 3e40c17 [HttpKernel] fixed locale management when exiting sub-requests Discussion ---------- [HttpKernel] fixed locale management when exiting sub-requests | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #7063 | License | MIT | Doc PR | n/a This fix is temporary as #7007 will fix it properly in Symfony 2.3. --------------------------------------------------------------------------- by vicb at 2013-02-17T20:17:44Z changelog ? --------------------------------------------------------------------------- by fabpot at 2013-02-17T20:27:22Z The changelogs are updated when we release a new version only. --------------------------------------------------------------------------- by stof at 2013-02-17T20:41:00Z @fabpot the intl locale should be reset to the right value too --------------------------------------------------------------------------- by stof at 2013-02-17T20:42:31Z hmm sorry, I missed the fact that you are changing the locale in the Request again, which will set the intl one
2 parents 6fb7904 + 3e40c17 commit 50c3de3

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

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

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

1414
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
15+
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
1516
use Symfony\Component\HttpKernel\KernelEvents;
17+
use Symfony\Component\HttpFoundation\Request;
1618
use Symfony\Component\Routing\RequestContextAwareInterface;
1719
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1820

@@ -25,33 +27,49 @@ class LocaleListener implements EventSubscriberInterface
2527
{
2628
private $router;
2729
private $defaultLocale;
30+
private $locales = array();
2831

2932
public function __construct($defaultLocale = 'en', RequestContextAwareInterface $router = null)
3033
{
3134
$this->defaultLocale = $defaultLocale;
3235
$this->router = $router;
3336
}
3437

38+
public function onKernelResponse(FilterResponseEvent $event)
39+
{
40+
array_shift($this->locales);
41+
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);
46+
}
47+
3548
public function onKernelRequest(GetResponseEvent $event)
3649
{
3750
$request = $event->getRequest();
3851

3952
$request->setDefaultLocale($this->defaultLocale);
53+
$this->setLocale($request, $request->attributes->get('_locale', $this->defaultLocale));
4054

41-
if ($locale = $request->attributes->get('_locale')) {
42-
$request->setLocale($locale);
43-
}
44-
45-
if (null !== $this->router) {
46-
$this->router->getContext()->setParameter('_locale', $request->getLocale());
47-
}
55+
array_unshift($this->locales, $request->getLocale());
4856
}
4957

5058
public static function getSubscribedEvents()
5159
{
5260
return array(
5361
// must be registered after the Router to have access to the _locale
5462
KernelEvents::REQUEST => array(array('onKernelRequest', 16)),
63+
KernelEvents::RESPONSE => 'onKernelResponse',
5564
);
5665
}
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+
}
5775
}

0 commit comments

Comments
 (0)