8000 fixed translator locale when dealing with sub-requests · symfony/symfony@0e65af2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0e65af2

Browse files
committed
fixed translator locale when dealing with sub-requests
1 parent 181e460 commit 0e65af2

File tree

3 files changed

+58
-18
lines changed

3 files changed

+58
-18
lines changed

src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,6 @@ public function __construct(ContainerInterface $container, MessageSelector $sele
6060
parent::__construct(null, $selector, $this->options['cache_dir'], $this->options['debug']);
6161
}
6262

63-
/**
64-
* {@inheritdoc}
65-
*/
66-
public function getLocale()
67-
{
68-
if (null === $this->locale && $request = $this->container->get('request_stack')->getCurrentRequest()) {
69-
$this->locale = $request->getLocale();
70-
try {
71-
$this->setLocale($request->getLocale());
72-
} catch (\InvalidArgumentException $e) {
73-
$this->setLocale($request->getDefaultLocale());
74-
}
75-
}
76-
77-
return $this->locale;
78-
}
79-
8063
/**
8164
* {@inheritdoc}
8265
*/
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 <fabien@symfony.com>
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 <fabien@symfony.com>
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->tra F0DC nslator->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+
}

src/Symfony/Component/HttpKernel/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)
0