<?php
namespace Aqarmap\Bundle\UserBundle\EventListener;
use FOS\UserBundle\Event\GetResponseUserEvent;
use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SetUserLanguageListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
FOSUserEvents::REGISTRATION_INITIALIZE => 'onRegistrationInitiate',
];
}
public function onRegistrationInitiate(GetResponseUserEvent $event): void
{
if ($request = $event->getRequest()->request) {
if ($registrationForm = $request->get('fos_user_registration_form')) {
$request->set(
'fos_user_registration_form',
array_merge($registrationForm, ['language' => $event->getRequest()->getLocale()])
);
} elseif ($registrationForm = $request->get('register')) {
$request->set(
'register',
array_merge($registrationForm, ['language' => $event->getRequest()->getLocale()])
);
}
}
}
}