title: Manage locale with Symfony3 LocaleSubscriber
body: <p>I've some troubles in using locale parameter with Symfony 3. I've tried a lot of things that I've found on Stack, but nothing seems to work.</p>
  
  <p>I've followed the official documentation : <a href="https://symfony.com/doc/current/session/locale_sticky_session.html" rel="nofollow noreferrer">https://symfony.com/doc/current/session/locale_sticky_session.html</a> </p>
  
  <h1>LocaleSubscriber.php</h1>
  
  <pre><code>&lt;?php
  
  namespace AppBundle\EventSubscriber;
  
  use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  use Symfony\Component\HttpKernel\KernelEvents;
  use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  
  use Symfony\Component\HttpKernel\HttpKernel;
  
  class LocaleSubscriber implements EventSubscriberInterface
  {
      private $defaultLocale;
  
      public function __construct($defaultLocale = 'en')
      {
          $this-&gt;defaultLocale = $defaultLocale;
      }
  
      public function onKernelRequest(GetResponseEvent $event)
      {
  
          if (HttpKernel::MASTER_REQUEST != $event-&gt;getRequestType()) {
              return;
          }
  
          $request = $event-&gt;getRequest();
          if (!$request-&gt;hasPreviousSession()) {
              return;
          }
  
          // try to see if the locale has been set as a _locale routing parameter
          if ($locale = $request-&gt;get('_locale')) {
              $request-&gt;set('_locale', $locale);
          } else {
              $request-&gt;set('_locale', $this-&gt;defaultLocale);
          }
      }
  
      public static function getSubscribedEvents()
      {
          return array(
              // must be registered after the default Locale listener
              KernelEvents::REQUEST =&gt; array(array('onKernelRequest', 17)),
          );
      }
  }
  </code></pre>
  
  <h1>config.yml</h1>
  
  <pre><code>framework:
  #esi: ~
  translator: { fallbacks: ['%locale%'] }
  </code></pre>
  
  <h1>service.yml</h1>
  
  <pre><code>AppBundle\EventSubscriber\LocaleSubscriber:
      arguments: ['%kernel.default_locale%']
      tags: [kernel.event_subscriber]
  </code></pre>
  
  <h1>AdminController</h1>
  
  <p>
  
  <pre><code>namespace AdminBundle\Controller;
  
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Symfony\Component\HttpFoundation\Request;
  
  class AdminController extends Controller
  {
      public function homeAction(Request $request)
      {
          var_dump($request-&gt;getLocale());
          var_dump($request-&gt;get('_locale'));
          return $this-&gt;render('AdminBundle:home:home.html.twig');
      }
  }
  </code></pre>
  
  <h1>HTML Links :</h1>
  
  <pre><code>&lt;li&gt;&lt;a href="{{-path(app.request.get('_route'), app.request.get('_route_params')|merge({'_locale' : 'fr'}))-}}"&gt;FR&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="{{-path(app.request.get('_route'), app.request.get('_route_params')|merge({'_locale' : 'en'}))-}}"&gt;EN&lt;/a&gt;&lt;/li&gt;
  </code></pre>
  
  <p><strong>Problem</strong> : When I try to switch of language, the parameter _locale is changed but the text don't switch language. "testfr" must be "testen" according to my messages.en.yml.</p>
  
  <p><strong>My var_dump when I click on the "FR" link :</strong> string(2) "fr" string(2) "fr"</p>
  
  <p><strong>My var_dump when I click on the "EN" link :</strong> string(2) "fr" string(2) "en"
  I guess it is not normal..</p>
  
  <p>If someone can help me, that would be nice and can remove a headache.</p>
  
  <p>Thanks</p>
  
  <p>Edit : The translator takes the "locale" parameter, which doesn't change when I click on one of theses links. I'm still investigating.</p>