wallabag/src/Wallabag/CoreBundle/Event/Listener/LocaleListener.php

45 lines
1.3 KiB
PHP
Raw Normal View History

2015-10-01 14:28:38 +00:00
<?php
namespace Wallabag\CoreBundle\Event\Listener;
2015-10-01 14:28:38 +00:00
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2015-10-01 14:28:38 +00:00
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
2015-10-16 05:40:09 +00:00
/**
* @see http://symfony.com/doc/current/cookbook/session/locale_sticky_session.html
*/
2015-10-01 14:28:38 +00:00
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 [
2015-10-01 14:28:38 +00:00
// must be registered before the default Locale listener
KernelEvents::REQUEST => [['onKernelRequest', 17]],
];
2015-10-01 14:28:38 +00:00
}
}