wallabag/src/Wallabag/CoreBundle/Event/Listener/UserLocaleListener.php

36 lines
1.1 KiB
PHP
Raw Normal View History

2015-10-01 14:28:38 +00:00
<?php
namespace Wallabag\CoreBundle\Event\Listener;
2015-10-01 14:28:38 +00:00
2022-11-14 22:11:46 +00:00
use Symfony\Component\HttpFoundation\Session\SessionInterface;
2015-10-01 14:28:38 +00:00
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
2022-11-23 14:51:33 +00:00
use Wallabag\UserBundle\Entity\User;
2015-10-01 14:28:38 +00:00
/**
* Stores the locale of the user in the session after the login.
* If no locale are defined (if user doesn't change it from the login screen), override it with the user's config one.
*
* This can be used by the LocaleListener afterwards.
2015-10-16 05:40:09 +00:00
*
* @see http://symfony.com/doc/master/cookbook/session/locale_sticky_session.html
2015-10-01 14:28:38 +00:00
*/
class UserLocaleListener
{
2022-11-14 22:11:46 +00:00
private SessionInterface $session;
2015-10-01 14:28:38 +00:00
2022-11-14 22:11:46 +00:00
public function __construct(SessionInterface $session)
2015-10-01 14:28:38 +00:00
{
$this->session = $session;
}
public function onInteractiveLogin(InteractiveLoginEvent $event)
{
$user = $event->getAuthenticationToken()->getUser();
2022-11-23 14:51:33 +00:00
\assert($user instanceof User);
2015-10-01 14:28:38 +00:00
if (null !== $user->getConfig()->getLanguage() && null === $this->session->get('_locale')) {
2015-10-01 14:28:38 +00:00
$this->session->set('_locale', $user->getConfig()->getLanguage());
}
}
}