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
This commit is contained in:
commit
b5572885ef
@ -60,23 +60,6 @@ class Translator extends BaseTranslator
|
|||||||
parent::__construct(null, $selector, $this->options['cache_dir'], $this->options['debug']);
|
parent::__construct(null, $selector, $this->options['cache_dir'], $this->options['debug']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function getLocale()
|
|
||||||
{
|
|
||||||
if (null === $this->locale && $request = $this->container->get('request_stack')->getCurrentRequest()) {
|
|
||||||
$this->locale = $request->getLocale();
|
|
||||||
try {
|
|
||||||
$this->setLocale($request->getLocale());
|
|
||||||
} catch (\InvalidArgumentException $e) {
|
|
||||||
$this->setLocale($request->getDefaultLocale());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->locale;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
@ -0,0 +1,56 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\HttpKernel\EventListener;
|
||||||
|
|
||||||
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
|
||||||
|
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
|
||||||
|
use Symfony\Component\HttpKernel\KernelEvents;
|
||||||
|
use Symfony\Component\HttpFoundation\RequestStack;
|
||||||
|
use Symfony\Component\Translator\TranslatorInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Synchronizes the locale between the request and the translator.
|
||||||
|
*
|
||||||
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
|
*/
|
||||||
|
class TranslatorListener implements EventSubscriberInterface
|
||||||
|
{
|
||||||
|
private $translator;
|
||||||
|
private $requestStack;
|
||||||
|
|
||||||
|
public function __construct(TranslatorInterface $translator, RequestStack $requestStack)
|
||||||
|
{
|
||||||
|
$this->translator = $translator;
|
||||||
|
$this->requestStack = $requestStack;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onKernelRequest(GetResponseEvent $event)
|
||||||
|
{
|
||||||
|
$this->translator->setLocale($event->getRequest()->getLocale());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onKernelFinishRequest(FinishRequestEvent $event)
|
||||||
|
{
|
||||||
|
if (null !== $parentRequest = $this->requestStack->getParentRequest()) {
|
||||||
|
$this->translator->setLocale($parentRequest->getLocale());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getSubscribedEvents()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
// must be registered after the Locale listener
|
||||||
|
KernelEvents::REQUEST => array(array('onKernelRequest', 10)),
|
||||||
|
KernelEvents::FINISH_REQUEST => array(array('onKernelFinishRequest', 0)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -32,7 +32,8 @@
|
|||||||
"symfony/process": "~2.0",
|
"symfony/process": "~2.0",
|
||||||
"symfony/routing": "~2.2",
|
"symfony/routing": "~2.2",
|
||||||
"symfony/stopwatch": "~2.2",
|
"symfony/stopwatch": "~2.2",
|
||||||
"symfony/templating": "~2.2"
|
"symfony/templating": "~2.2",
|
||||||
|
"symfony/translator": "~2.0"
|
||||||
},
|
},
|
||||||
"suggest": {
|
"suggest": {
|
||||||
"symfony/browser-kit": "",
|
"symfony/browser-kit": "",
|
||||||
|
Reference in New Issue
Block a user