In services.yml
you just need to add @translator
service reference
services.yml
services:
mylistener:
class: Your\Bundle\Listener\RequestListener
arguments: [ @translator ]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
Your/Bundle/Listener/RequestListener.php
namespace Your\Bundle\Listener;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
class RequestListener
{
protected $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
public function onKernelRequest(GetResponseEvent $event)
{
$this->translator->trans('key_name');
}
}