symfony - Symfony2 locale detection: not considering _locale in session -


i'm trying implement localelistener detects user's preferred language (considering accept-language header) , stores in session avoid checking every request. i've developed code below accomplish this:

public function onkernelrequest(getresponseevent $event) {     $request = $event->getrequest();      if (httpkernelinterface::master_request !== $event->getrequesttype()) {         return;     }      $preferredlocale = $request->getpreferredlanguage($this->availablelocales);      if ($this->container->has('session')) {         $session = $this->container->get('session');         if (!$session->has('_locale')) {             $session->set('_locale', $preferredlocale);         }     } else {         $request->setlocale($preferredlocale);     } } 

the code working, preferred language being stored in session, symfony isn't considering locale stored in session translate strings. in case, preferred language 'pt_br' , when escape:

{{ app.request.locale }} 

symfony escaping 'en'. shouldn't symfony considering value stored in session('_locale') define request locale? correct behavior? how can accomplish that?

here working language listener. second method change language users preferences, user chooses. can omit method, if user haven't facility define language.

<?php  namespace acme\userbundle\eventlistener; use symfony\component\httpfoundation\session\session; use symfony\component\security\http\event\interactiveloginevent; use symfony\component\httpkernel\event\getresponseevent; use symfony\component\httpkernel\httpkernelinterface;  class languagelistener {     private $session;      public function setsession(session $session)     {         $this->session = $session;     }      /**      * kernel.request event. if guest user doesn't have opened session, locale equal      * "undefined" configured default in parameters.ini. if so, set locale user's      * preferred language.      *      * @param \symfony\component\httpkernel\event\getresponseevent $event      */     public function setlocaleforunauthenticateduser(getresponseevent $event)     {         if (httpkernelinterface::master_request !== $event->getrequesttype()) {             return;         }         $request = $event->getrequest();         if ('undefined' == $request->getlocale()) {             if ($locale = $request->getsession()->get('_locale')) {                 $request->setlocale($locale);             } else {                 $request->setlocale($request->getpreferredlanguage());             }         }     }      /**      * security.interactive_login event. if user chose language in preferences, set,      * if not, locale set setlocaleforunauthenticateduser remains.      *      * @param \symfony\component\security\http\event\interactiveloginevent $event      */     public function setlocaleforauthenticateduser(interactiveloginevent $event)     {         $user = $event->getauthenticationtoken()->getuser();          if ($lang = $user->getlanguage()) {             $this->session->set('_locale', $lang);         }     } } 

in services.yml:

services:     acme.language.interactive_login_listener:         class: acme\userbundle\eventlistener\languagelistener         calls:             - [ setsession, [@session] ]         tags:             - { name: kernel.event_listener, event: security.interactive_login, method: setlocaleforauthenticateduser }      acme.language.kernel_request_listener:         class: acme\userbundle\eventlistener\languagelistener         tags:             - { name: kernel.event_listener, event: kernel.request, method: setlocaleforunauthenticateduser } 

oh, , have define undefined fallback_language in config.yml work.

framework:     translator:      { fallback: "undefined" }     default_locale:  "en" 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -