我正在尝试使用symfony sticky locale来通过URL更改Web区域设置。为此,在创建事件订阅者之前先链接页面y,该事件订阅者在onKernelRequest函数上实现此功能。
事件子程序正在运行,并且正确地执行了if($ loc)阻止,但是翻译器似乎未检测到它,因此这可能是事件子用户优先级问题,但是我尝试更改其优先级同一本地人。
事件订阅者
<?php
// src/EventSubscriber/LocaleSubscriber.php
namespace App\EventSubscriber;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class LocaleSubscriber implements EventSubscriberInterface
{
private $defaultLocale;
public function __construct($defaultLocale = 'en')
{
$this->defaultLocale = $defaultLocale;
}
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if (!$request->hasPreviousSession()) {
return;
}
$loc = $request->attributes->get('_locale');
// try to see if the locale has been set as a _locale routing parameter
if ($loc) {
$request->getSession()->set('_locale', $loc);
$request->setLocale($loc);
} else {
// if no explicit locale has been set on this request, use one from the session
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
}
}
public static function getSubscribedEvents()
{
return [
// must be registered before (i.e. with a higher priority than) the default Locale listener
KernelEvents::REQUEST => [['onKernelRequest', 20]],
];
}
}
控制器
/**
* @Route("/{_locale}/pro/live")
*/
public function index(Request $request)
{
return $this->render('pro/live/index.html.twig');
}
我希望更改网址中写入的翻译器语言环境。
如果我在控制器中将URL更改为/en/pro/live/
$request->getLocale()
,则返回正确的语言环境,但翻译器本地语言仍在es
答案 0 :(得分:0)
我解决了这个问题,previos开发人员创建了一个事件订阅者来更改翻译器的语言环境。 Whitout效果很好