]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/EventListener/LocaleListener.php
Merge pull request #1524 from wallabag/sf2.8
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / EventListener / LocaleListener.php
CommitLineData
c89d35e8
NL
1<?php
2
3namespace Wallabag\CoreBundle\EventListener;
4
619cc453 5use Symfony\Component\EventDispatcher\EventSubscriberInterface;
c89d35e8
NL
6use Symfony\Component\HttpKernel\Event\GetResponseEvent;
7use Symfony\Component\HttpKernel\KernelEvents;
c89d35e8 8
2aac2f27
JB
9/**
10 * @see http://symfony.com/doc/current/cookbook/session/locale_sticky_session.html
11 */
c89d35e8
NL
12class LocaleListener implements EventSubscriberInterface
13{
14 private $defaultLocale;
15
16 public function __construct($defaultLocale = 'en')
17 {
18 $this->defaultLocale = $defaultLocale;
19 }
20
21 public function onKernelRequest(GetResponseEvent $event)
22 {
23 $request = $event->getRequest();
24 if (!$request->hasPreviousSession()) {
25 return;
26 }
27
28 // try to see if the locale has been set as a _locale routing parameter
29 if ($locale = $request->attributes->get('_locale')) {
30 $request->getSession()->set('_locale', $locale);
31 } else {
32 // if no explicit locale has been set on this request, use one from the session
33 $request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
34 }
35 }
36
37 public static function getSubscribedEvents()
38 {
39 return array(
40 // must be registered before the default Locale listener
41 KernelEvents::REQUEST => array(array('onKernelRequest', 17)),
42 );
43 }
44}