wallabag/src/Wallabag/CoreBundle/Event/Listener/LocaleListener.php
2016-10-30 09:58:39 +01:00

45 lines
1.3 KiB
PHP

<?php
namespace Wallabag\CoreBundle\Event\Listener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* @see http://symfony.com/doc/current/cookbook/session/locale_sticky_session.html
*/
class LocaleListener implements EventSubscriberInterface
{
private $defaultLocale;
public function __construct($defaultLocale = 'en')
{
$this->defaultLocale = $defaultLocale;
}
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if (!$request->hasPreviousSession()) {
return;
}
// try to see if the locale has been set as a _locale routing parameter
if ($locale = $request->attributes->get('_locale')) {
$request->getSession()->set('_locale', $locale);
} else {
// if no explicit locale has been set on this request, use one from the session
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
}
}
public static function getSubscribedEvents()
{
return [
// must be registered before the default Locale listener
KernelEvents::REQUEST => [['onKernelRequest', 17]],
];
}
}