diff --git a/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php b/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php index 996cbf8672..e2924b96f8 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php +++ b/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php @@ -60,23 +60,6 @@ class Translator extends BaseTranslator 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} */ diff --git a/src/Symfony/Component/HttpKernel/EventListener/TranslatorListener.php b/src/Symfony/Component/HttpKernel/EventListener/TranslatorListener.php new file mode 100644 index 0000000000..b682b3fc49 --- /dev/null +++ b/src/Symfony/Component/HttpKernel/EventListener/TranslatorListener.php @@ -0,0 +1,56 @@ + + * + * 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 + */ +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)), + ); + } +} diff --git a/src/Symfony/Component/HttpKernel/composer.json b/src/Symfony/Component/HttpKernel/composer.json index 2649b71a6b..4ba7086150 100644 --- a/src/Symfony/Component/HttpKernel/composer.json +++ b/src/Symfony/Component/HttpKernel/composer.json @@ -32,7 +32,8 @@ "symfony/process": "~2.0", "symfony/routing": "~2.2", "symfony/stopwatch": "~2.2", - "symfony/templating": "~2.2" + "symfony/templating": "~2.2", + "symfony/translator": "~2.0" }, "suggest": { "symfony/browser-kit": "",