diff --git a/cookbook/session/locale_sticky_session.rst b/cookbook/session/locale_sticky_session.rst
index 2aa61eed497..44a323d6b80 100644
--- a/cookbook/session/locale_sticky_session.rst
+++ b/cookbook/session/locale_sticky_session.rst
@@ -106,3 +106,112 @@ method::
{
$locale = $request->getLocale();
}
+
+Setting the Locale Based on the User's Preferences
+--------------------------------------------------
+
+You might want to improve this technique even further and define the locale based on
+the user entity of the logged in user. However, since the ``LocaleListener`` is called
+before the ``FirewallListener``, which is responsible for handling authentication and
+is setting the user token into the ``TokenStorage``, you have no access to the user
+which is logged in.
+
+Let's pretend you have defined a property "locale" in your user entity which you
+want to be used as the locale for the given user. In order to achieve the wanted locale
+configuration, you can set the locale which is defined for the user to the session right
+after the login. Fortunately, you can hook into the login process and update the user's
+session before the redirect to the first page. For this you need an event listener for the
+``security.interactive_login`` event:
+
+.. code-block:: php
+
+ // src/AppBundle/EventListener/UserLocaleListener.php
+ namespace AppBundle\EventListener;
+
+ use Symfony\Component\HttpFoundation\Session\Session;
+ use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
+
+ /**
+ * Stores the locale of the user in the session after the
+ * login. This can be used by the LocaleListener afterwards.
+ */
+ class UserLocaleListener
+ {
+ /**
+ * @var Session
+ */
+ private $session;
+
+ public function __construct(Session $session)
+ {
+ $this->session = $session;
+ }
+
+ /**
+ * @param InteractiveLoginEvent $event
+ */
+ public function onInteractiveLogin(InteractiveLoginEvent $event)
+ {
+ $user = $event->getAuthenticationToken()->getUser();
+
+ if (null !== $user->getLocale()) {
+ $this->session->set('_locale', $user->getLocale());
+ }
+ }
+ }
+
+Then register the listener:
+
+.. configuration-block::
+
+ .. code-block:: yaml
+
+ # app/config/services.yml
+ services:
+ app.user_locale_listener:
+ class: AppBundle\EventListener\UserLocaleListener
+ arguments: [@session]
+ tags:
+ - { name: kernel.event_listener, event: security.interactive_login, method: onInteractiveLogin }
+
+ .. code-block:: xml
+
+
+
+
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: