]>
Commit | Line | Data |
---|---|---|
c89d35e8 NL |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\EventListener; | |
4 | ||
5 | use Symfony\Component\HttpKernel\Event\GetResponseEvent; | |
6 | use Symfony\Component\HttpKernel\KernelEvents; | |
7 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
8 | ||
9 | class LocaleListener implements EventSubscriberInterface | |
10 | { | |
11 | private $defaultLocale; | |
12 | ||
13 | public function __construct($defaultLocale = 'en') | |
14 | { | |
15 | $this->defaultLocale = $defaultLocale; | |
16 | } | |
17 | ||
18 | public function onKernelRequest(GetResponseEvent $event) | |
19 | { | |
20 | $request = $event->getRequest(); | |
21 | if (!$request->hasPreviousSession()) { | |
22 | return; | |
23 | } | |
24 | ||
25 | // try to see if the locale has been set as a _locale routing parameter | |
26 | if ($locale = $request->attributes->get('_locale')) { | |
27 | $request->getSession()->set('_locale', $locale); | |
28 | } else { | |
29 | // if no explicit locale has been set on this request, use one from the session | |
30 | $request->setLocale($request->getSession()->get('_locale', $this->defaultLocale)); | |
31 | } | |
32 | } | |
33 | ||
34 | public static function getSubscribedEvents() | |
35 | { | |
36 | return array( | |
37 | // must be registered before the default Locale listener | |
38 | KernelEvents::REQUEST => array(array('onKernelRequest', 17)), | |
39 | ); | |
40 | } | |
41 | } |