简体   繁体   English

基本树枝模板忽略了Symfony2语言环境

[英]Symfony2 locale ignored for base twig template

Situation 情况

I'm setting locale on a listener listening on kernel.request like this: 我正在像这样在监听kernel.request的监听器上设置语言环境:

<?php
// ...
class LocaleListener
{
    //...
    public function onKernelRequest(GetResponseEvent $event)
    {
        $request = $event->getRequest();
        if (!$event->isMasterRequest()) {
            return;
        }

        // [...] some logic to get $locale
        $request->setLocale($locale);
    }
}
?>

Then, I have two twig templates, a base and a base-extending-template. 然后,我有两个树枝模板,一个基础和一个基础扩展模板。 Base template ( base.html.twig ): 基本模板( base.html.twig ):

<html>
<body>
    {{ 'some.translation'|trans({}, 'messages') }}

    {% block body %}{% endblock %}
</body>
</html>

And the base-extending-template ( profile.html.twig ): 和基本扩展模板( profile.html.twig ):

{% extends '::base.html.twig' %}

{% block body %}
    {{ 'some.translation'|trans({}, 'messages') }}
{% endblock %}

The Controller of the route will render profile.html.twig: 路线的控制器将呈现profile.html.twig:

<?php
// ...
class SomeController extends Controller
{
    //...
    public function someAction()
    {
        return $this->render('::profile.html.twig');
    }
}
?>

Nothing special, all very simple... 没什么特别的,都非常简单...

Problem 问题

The rendered template extending the base ( profile.html.twig ) is using the locale for the translation set in the listener. 扩展基础( profile.html.twig )的呈现模板正在使用语言环境进行侦听器中设置的翻译。 Unfortunately the base template ( base.html.twig ) IS NOT. 不幸的是,基本模板( base.html.twig )不是。 Even when I dump {{ dump(app.request.locale) }} on the base template, it shows the correct locale set in the listener... 即使我在基本模板上转储{{ dump(app.request.locale) }}时,它{{ dump(app.request.locale) }}显示在侦听器中设置的正确语言环境...

What do I miss? 我想念什么?


Edit 编辑

Oh, and I'm using Symfony v2.7.11. 哦,我正在使用Symfony v2.7.11。

Working solution : Created the LocaleListener exactly like documented here (hint by Yoshi). 工作解决方案 :完全按照此处记录的方式创建了LocaleListener(Yoshi提示)。

Addendum : I wanted the locale stored in User's entity to be used, so I had to create a InteractiveLoginListener ( class file ). 附录 :我希望使用存储在User实体中的语言环境,因此必须创建一个InteractiveLoginListener类文件 )。 As of version 1.3.6, there's (like 2.0-alpha, even though not documented) an event called security.interactive_login which had to be injected: 从1.3.6版开始,有一个(称为2.0-alpha,即使没有记录)事件,称为security.interactive_login ,该事件必须注入:

app.interactive_login_listener:
    class: AppBundle\EventListener\Security\InteractiveLoginListener
    arguments: ['@fos_user.user_manager', '@session']
    tags:
        - { name: 'kernel.event_listener', event: 'security.interactive_login', method: 'onSecurityInteractiveLogin' }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM