Skip to content

Instantly share code, notes, and snippets.

@wpottier
Last active December 17, 2015 20:48
Show Gist options
  • Save wpottier/5670027 to your computer and use it in GitHub Desktop.
Save wpottier/5670027 to your computer and use it in GitHub Desktop.
<?php
namespace Formation\CoreBundle\Listener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class LocaleListener implements EventSubscriberInterface
{
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if(!$request->getSession()->has('locale')) {
$request->getSession()->set('locale', $request->getPreferredLanguage());
}
$request->setLocale($request->getSession()->get('locale'));
}
public static function getSubscribedEvents()
{
return array(KernelEvents::REQUEST => array(array('onKernelRequest', 17)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment