我使用Symfony 2.6.0 我需要设置用户的语言而不是默认语言。 更改区域设置的服务:
helper.interactive_login_listener:
class: HelperBundle\Listeners\LanguageListener
calls:
- [ setSession, [@session] ]
tags:
- { name: kernel.event_listener, event: security.interactive_login, method: setLocaleForAuthenticatedUser }
helper.language.kernel_request_listener:
class: HelperBundle\Listeners\LanguageListener
tags:
- { name: kernel.event_listener, event: kernel.request, method: setLocaleForUnauthenticatedUser }
代码:
namespace HelperBundle\Listeners;
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 a guest user doesn't have an opened session, locale is equal to
* "undefined" as configured by default in parameters.ini. If so, set as a locale the 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();
$request->setLocale('ru');
}
/**
* security.interactive_login event. If a user chose a language in preferences, it would be set,
* if not, a locale that was set by setLocaleForUnauthenticatedUser remains.
*
* @param \Symfony\Component\Security\Http\Event\InteractiveLoginEvent $event
*/
public function setLocaleForAuthenticatedUser(InteractiveLoginEvent $event)
{
$this->session->set('_locale', 'ru');
}
}
配置:
framework:
translator: ~
default_locale: ~
在控制器中:
namespace HelperBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
abstract class PrototypeController extends Controller
{
protected $model;
protected $flashBag;
//Метод загрузки модели контроллера
abstract function loadServiceModelEvent($model_service_name);
//Вывод шаблона
protected function renderPage($template, $response)
{
print $this->get('request')->getLocale().'==';
print $this->get('request')->getSession()->get('_locale'); die();
结果打印:
ru==ru
同时使用英语(en)。 但是,如果设置
framework:
translator: ~
default_locale: ru
然后一切都很好。 如何改变语言? 谢谢!
答案 0 :(得分:0)
如果我正确理解了这个问题,问题是听众没有正确设置语言。在这种情况下,侦听器的问题是,您无法为其执行设置优先级。那是EventSubscriber来参加聚会的时候。
# app/config/services.yml
services:
app.exception_subscriber:
class: AppBundle\EventSubscriber\LanguageListener
tags:
- { name: kernel.event_subscriber }
在这种情况下你的听众会这样:
class LanguageListener implements EventSubscriberInterface
{
private $session;
public function setSession(Session $session)
{
$this->session = $session;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => [
['onKernelRequest', 9],
],
];
}
public function onKernelRequest(GetResponseEvent $event)
{
...
}
}
正如您所看到的差异并不大,但在getSubscribedEvents
函数中,您可以指定将使用的优先级(在本例中为9)。您可以在official documentation中了解有关优先级的更多信息。
您需要确保在Symfony框架初始化LocaleListener
之前执行订阅者。该侦听器(实际上是订阅者)使用设置“搞乱”并设置默认值,如果有的话。因此,如果您之前拦截了语言环境,它应该可以正常工作。
您可以通过CLI命令检查所有已注册的侦听器和订阅者:
php bin/console debug:event-dispatcher