我在一个Symfony(2.7)应用程序的登录事件中遇到了更改语言环境的问题。我正在遵循"sticky locale Symfony article"中的步骤 http://symfony.com/doc/current/cookbook/session/locale_sticky_session.html,但根本没有使用会话。
我采用的方法是每个URI只包含一种表示形式。因此,所有路由都包含{_locale}(不需要为粘性语言环境设置会话变量)。
我只使用UserLocaleListener部分来获取用户语言环境,并尝试像这个GitHub问题https://github.com/FriendsOfSymfony/FOSUserBundle/issues/829中所述将其设置到路由中。
我的问题可能是,在登录时已经设置了URI语言环境。在登录事件期间,我需要/想要覆盖此语言环境值。如GitHub文章所述,我添加了router context。
UserLocaleListener InteractiveLoginEvent
我采用的方法是每个URI只包含一种表示形式。因此,所有路由都包含{_locale}(不需要为粘性语言环境设置会话变量)。
我只使用UserLocaleListener部分来获取用户语言环境,并尝试像这个GitHub问题https://github.com/FriendsOfSymfony/FOSUserBundle/issues/829中所述将其设置到路由中。
我的问题可能是,在登录时已经设置了URI语言环境。在登录事件期间,我需要/想要覆盖此语言环境值。如GitHub文章所述,我添加了router context。
UserLocaleListener InteractiveLoginEvent
/**
* @param InteractiveLoginEvent $event
*/
public function onInteractiveLogin(InteractiveLoginEvent $event)
{
$request = $event->getRequest();
$user = $event->getAuthenticationToken()->getUser();
$locale = $user->getLocale();
if (null !== $locale) {
$request->setLocale($locale);
$this->router->getContext()->setParameter('_locale', $locale);
//var_dump(); die;
}
}
很遗憾,它不起作用(至少在我的Symfony 2.7上)。
如果我转储请求语言环境或路由器上下文语言环境,则正确的用户语言环境已应用于上下文,但在重定向到default_target_path或任何其他路径时未更改/应用。
onInteractiveLoginEvent是否已经太晚在URI中更改语言环境?还是在(基于表单的)登录期间如何最好地更改URI中的语言环境?
非常感谢任何提示。