我正在尝试使用symfony sticky locale通过url更改Web区域设置。对于这个,正如在我创建了一个在onKernelRequest函数上实现它的事件订阅者之前链接的页面y所解释的那样。
事件子工程正在运行并正确执行de 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中写入的转换器语言环境。
如果我在控制器中将url更改为/en/pro/live/
$request->getLocale()
,则返回正确的语言环境,但本地翻译仍在es
中
我解决了这个问题,之前的开发人员创建了一个更改转换器语言环境的事件订阅者。没有它完美的工作