Soanta Admin Bundle 语言环境 [英] Soanta Admin Bundle Locale
问题描述
我有翻译问题.我用
- symfony 2.7
- 奏鸣曲管理包 2.3
我创建了交互式登录侦听器,当用户登录应用程序时,我获取用户区域设置并设置会话 _locale,但这在奏鸣曲中被忽略.
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.
听众是
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'));
}
}
}
在 service.yml 中添加
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 }
我的错误在哪里?
推荐答案
区域设置是在请求上设置的,不会粘住",因此每个请求都将再次成为默认设置,除非您执行以下操作:
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 将针对每个请求,从会话中获取语言环境,并将其设置在当前请求上.
Wich will on each request, take the locale from the session, and set it on the current request.
(确保 LocaleListener 的优先级低于您的 UserLocaleListener,以便它在它之后运行)
(make sure that LocaleListener has a lower priority then your UserLocaleListener, so that it runs after it)
这篇关于Soanta Admin Bundle 语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!