Skip to content

Commit 69ee4de

Browse files
committed
feature #12046 fixed translator locale when dealing with sub-requests (fabpot)
This PR was merged into the 2.6-dev branch. Discussion ---------- fixed translator locale when dealing with sub-requests | Q | A | ------------- | --- | Bug fix? | yes, kinda | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a This fixes the (edge) case where the locale of a sub-requests is different from the locale of the master request. The listener synchronizes the translator locale with the one from the request. Commits ------- 0e65af2 fixed translator locale when dealing with sub-requests
2 parents 5402bad + 48ffdad commit 69ee4de

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

EventListener/TranslatorListener.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\EventListener;
13+
14+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
15+
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
16+
use Symfony\Component\HttpKernel\KernelEvents;
17+
use Symfony\Component\HttpFoundation\RequestStack;
18+
use Symfony\Component\Translator\TranslatorInterface;
19+
20+
/**
21+
* Synchronizes the locale between the request and the translator.
22+
*
23+
* @author Fabien Potencier <[email protected]>
24+
*/
25+
class TranslatorListener implements EventSubscriberInterface
26+
{
27+
private $translator;
28+
private $requestStack;
29+
30+
public function __construct(TranslatorInterface $translator, RequestStack $requestStack)
31+
{
32+
$this->translator = $translator;
33+
$this->requestStack = $requestStack;
34+
}
35+
36+
public function onKernelRequest(GetResponseEvent $event)
37+
{
38+
$this->translator->setLocale($event->getRequest()->getLocale());
39+
}
40+
41+
public function onKernelFinishRequest(FinishRequestEvent $event)
42+
{
43+
if (null !== $parentRequest = $this->requestStack->getParentRequest()) {
44+
$this->translator->setLocale($parentRequest->getLocale());
45+
}
46+
}
47+
48+
public static function getSubscribedEvents()
49+
{
50+
return array(
51+
// must be registered after the Locale listener
52+
KernelEvents::REQUEST => array(array('onKernelRequest', 10)),
53+
KernelEvents::FINISH_REQUEST => array(array('onKernelFinishRequest', 0)),
54+
);
55+
}
56+
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"symfony/process": "~2.0",
3333
"symfony/routing": "~2.2",
3434
"symfony/stopwatch": "~2.2",
35-
"symfony/templating": "~2.2"
35+
"symfony/templating": "~2.2",
36+
"symfony/translator": "~2.0"
3637
},
3738
"suggest": {
3839
"symfony/browser-kit": "",

0 commit comments

Comments
 (0)