0
votes

I have a problem with translation. I use

  • symfony 2.7
  • sonata admin-bundle 2.3

I have create the interactive login listener, when the user log in the application I get the user locale and set the session _locale, but this is ignore in sonata.

Listener is

class UserLocaleListener {

 /**
  * @var Session
  */
private $container;


public function __construct(Session $session)
{
    $this->session = $session;
}

public function onInteractiveLogin(InteractiveLoginEvent $event)
{
    $request = $event->getRequest();

    $user = $event->getAuthenticationToken()->getUser();


    if (null !== $user->getLocale()) {
        $this->session->set('_locale', $user->getLocale());
        //$request->setLocale($user->getLocale());
        var_dump($request->getSession()->get('_locale'));
    }
  }
}

in service.yml add

app.user_locale_listener:
    class: xxxxxx\xxxxxxxx\EventListener\UserLocaleListener
    arguments: ["@session"]
    tags:
        - { name: kernel.event_listener, event: security.interactive_login,       method: onInteractiveLogin }

Where is my mistake ?

1

1 Answers

1
votes

the locale is set on the request, and will not "stick" , so each request it will be the default again, unless you do something like this:

http://symfony.com/doc/current/cookbook/session/locale_sticky_session.html

Wich will on each request, take the locale from the session, and set it on the current request.

(make sure that LocaleListener has a lower priority then your UserLocaleListener, so that it runs after it)