3 namespace Wallabag\CoreBundle\Event\Listener
;
5 use Symfony\Component\EventDispatcher\EventSubscriberInterface
;
6 use Symfony\Component\HttpKernel\Event\GetResponseEvent
;
7 use Symfony\Component\HttpKernel\KernelEvents
;
10 * @see http://symfony.com/doc/current/cookbook/session/locale_sticky_session.html
12 class LocaleListener
implements EventSubscriberInterface
14 private $defaultLocale;
16 public function __construct($defaultLocale = 'en')
18 $this->defaultLocale
= $defaultLocale;
21 public function onKernelRequest(GetResponseEvent
$event)
23 $request = $event->getRequest();
24 if (!$request->hasPreviousSession()) {
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);
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
));
37 public static function getSubscribedEvents()
40 // must be registered before the default Locale listener
41 KernelEvents
::REQUEST
=> [['onKernelRequest', 17]],