Symfony - 在 404 页面上使用 twig trans 过滤器
Symfony - using twig trans filter on 404 page
我通过内核异常触发的事件侦听器设置了 404 错误页面:
public function onKernelException(GetResponseForExceptionEvent $event)
{
if ($event->isMasterRequest()) {
$exception = $event->getException();
if ($exception instanceof NotFoundHttpException) {
$response = new Response();
$event->setResponse(
$response->setContent($this->templating->render(
'LandingPageBundle:Error:error404.html.twig',
['welcome_url' => $this->router->generate("welcome")]
))
);
}
}
}
kernel.kernel_exception_listener:
class: S\Project\LandingPageBundle\EventListener\KernelExceptionListener
arguments: [ "@router", "@logger", "@translator", @templating ]
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
这有效,但是当我尝试在 twig error404.html.twig 模板上使用 trans 过滤器时,它什么也没做。我的区域设置在 cookie 中设置并由事件侦听器读取到内核请求,因此我尝试将以下内容添加到 onKernelException:
$request = $event->getRequest();
$locale = $request->cookies->get('_locale');
$request->setLocale($locale);
之后,在模板中包含 {{ app.request.locale }}
显示了正确的语言环境,但是,trans 过滤器似乎并没有选择它。
看来我的问题可能与Symfony 2.1 set locale and yet that question does not quite fit my exact problem, and I'm not sure what can be done to fix the problem. Ideally my kernel request listener could trigger before the onKernelException, so that it would properly set the locale beforehand, but currently it seems that the kernel request event is not triggered during a 404. I took a look at http://symfony.com/doc/current/components/http_kernel/introduction.html to better understand Symfony's request sequence, but I'm not really clear on the sequence that happens in a bad request, but it looks like in the case of an exception, it skips most of the request flow and goes straight to the response, and as I recall from http://symfony.com/doc/current/book/translation.html#handling-the-user-s-locale
有关
"Setting the locale using $request->setLocale() in the controller is too late to affect the translator. Either set the locale via a listener (like above), the URL (see next) or call setLocale() directly on the translator service."
有没有办法在 twig 模板化 404 页面上使用 trans 过滤器?
尝试注入 @translator
并使用其方法 setLocale
。
['welcome_url' => $this->router->generate("welcome")]
为什么要在侦听器中创建 link?您应该使用名为 path
.
的 twig 函数在模板中执行此操作
我通过内核异常触发的事件侦听器设置了 404 错误页面:
public function onKernelException(GetResponseForExceptionEvent $event)
{
if ($event->isMasterRequest()) {
$exception = $event->getException();
if ($exception instanceof NotFoundHttpException) {
$response = new Response();
$event->setResponse(
$response->setContent($this->templating->render(
'LandingPageBundle:Error:error404.html.twig',
['welcome_url' => $this->router->generate("welcome")]
))
);
}
}
}
kernel.kernel_exception_listener:
class: S\Project\LandingPageBundle\EventListener\KernelExceptionListener
arguments: [ "@router", "@logger", "@translator", @templating ]
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
这有效,但是当我尝试在 twig error404.html.twig 模板上使用 trans 过滤器时,它什么也没做。我的区域设置在 cookie 中设置并由事件侦听器读取到内核请求,因此我尝试将以下内容添加到 onKernelException:
$request = $event->getRequest();
$locale = $request->cookies->get('_locale');
$request->setLocale($locale);
之后,在模板中包含 {{ app.request.locale }}
显示了正确的语言环境,但是,trans 过滤器似乎并没有选择它。
看来我的问题可能与Symfony 2.1 set locale and yet that question does not quite fit my exact problem, and I'm not sure what can be done to fix the problem. Ideally my kernel request listener could trigger before the onKernelException, so that it would properly set the locale beforehand, but currently it seems that the kernel request event is not triggered during a 404. I took a look at http://symfony.com/doc/current/components/http_kernel/introduction.html to better understand Symfony's request sequence, but I'm not really clear on the sequence that happens in a bad request, but it looks like in the case of an exception, it skips most of the request flow and goes straight to the response, and as I recall from http://symfony.com/doc/current/book/translation.html#handling-the-user-s-locale
有关"Setting the locale using $request->setLocale() in the controller is too late to affect the translator. Either set the locale via a listener (like above), the URL (see next) or call setLocale() directly on the translator service."
有没有办法在 twig 模板化 404 页面上使用 trans 过滤器?
尝试注入 @translator
并使用其方法 setLocale
。
['welcome_url' => $this->router->generate("welcome")]
为什么要在侦听器中创建 link?您应该使用名为 path
.