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
:
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?