Soanta Admin Bundle Locale

核能气质少年 提交于 2019-12-02 19:13:05

问题


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:


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)



来源:https://stackoverflow.com/questions/32892440/soanta-admin-bundle-locale

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!