douqian9729 2019-04-20 19:18
浏览 149

Symfony 3.4如何设置语言环境

Using symfony 3.4 I am setting the locale using an Event Listener with higher priority than LocaleListener as descibed in the symfony docs:

namespace AppBundle\EventListener;

use AppBundle\Component\Common;
use AppBundle\Entity\Client;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

class EventListener
{
    private $entityManager;

    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public function onKernelRequest(GetResponseEvent $event)
    {
        if ($this->entityManager !== null) {
            $client = Common::getClient($this->entityManager);
            $event->getRequest()->setLocale($client->getDefaultLocale());
        }
    }
}

Now to my understanding, to get the locale in a twig template, I use

{{ app.request.locale }}

which outputs the locale which I set via the EventListener (namely en). But I also implement the getDisplayLocale() method on my Member-object with which I log in to the page. The idea here is that without login, the default locale of the client should be used, if logged in, the locale of the member. Now the member locale is de.

The wired thing I do not understand now is, that the page is actually rendered in German and the symfony status bar shows german, even thought the twig template variable {{ app.request.locale }} shows en:

symfony locale and app.request.locale

Why is the variable shown in the symfony status bar and the one from {{ app.request.locale }} not the same? How do I set the variable that is actually used for the translation in my twig template in the EventListener?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办
    • ¥15 kylin启动报错log4j类冲突
    • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
    • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序