title: symfony 2 set locale based on user preferences stored in DB
body: <p>I am trying to set the locale based on the current user's preferences which are stored in the DB. </p>
  
  <p>Our User class therefore has a getPreferredLanguage which returns a locale identify ('en', 'fr_FR', etc.).</p>
  
  <p>I've considered the following approach:</p>
  
  <ul>
  <li>register a "locale" listener service that subscribes to the KernelEvents::REQUEST event. </li>
  <li>this service has access to the security context (via its constructor)</li>
  <li>this service's onKernelRequest method attempts to get the user from the security context, get the user's preferred locale, and set it as the request's locale.</li>
  </ul>
  
  <p>Unfortunately, this doesn't work. When the "locale" listener service's onRequestEvent method is invoked, the security context does not have a token. It seems that the context listener is invoked at a very late stage (with a priority of 0), and it is impossible to tell my "locale" listener to run before the security context.</p>
  
  <p>Does anyone know how to fix this approach, or suggest another one?</p>