Skip to content

Commit 66e21a9

Browse files
committed
Added chapter about the locale based on the user entity
1 parent 9fee9ee commit 66e21a9

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

cookbook/session/locale_sticky_session.rst

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,86 @@ method::
106106
{
107107
$locale = $request->getLocale();
108108
}
109+
110+
Setting the locale based on the user entity
111+
-------------------------------------------
112+
113+
You might want to improve even further and want to define the locale based on
114+
the user entity of the logged in user. However since the `LocaleListener` is called
115+
before the `FirewallListener`, which is responsible for handling authentication and
116+
is setting the user token into the `TokenStorage`, you have no access to the user
117+
which is logged in.
118+
119+
First lets pretend you have defined a property locale in your User Entity which you
120+
want to be used as the locale for the given user. In order to achieve the wanted locale
121+
configuration you can set the locale which is defined for the user to the session right
122+
after the login. Fortunately you can hook into the login process and update your session
123+
variable before the redirect to the first page. For this you need an event listener for the
124+
`security.interactive_login` event.
125+
126+
// src/AppBundle/EventListener/UserLocaleListener.php
127+
namespace AppBundle\EventListener;
128+
129+
use Symfony\Component\HttpFoundation\Session\Session;
130+
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
131+
132+
/**
133+
* Stores the locale of the user in the session after the
134+
* login. This can be used by the LocaleListener afterwards.
135+
*/
136+
class UserLocaleListener
137+
{
138+
/**
139+
* @var Session
140+
*/
141+
private $session;
142+
public function __construct(Session $session)
143+
{
144+
$this->session = $session;
145+
}
146+
/**
147+
* @param InteractiveLoginEvent $event
148+
*/
149+
public function onInteractiveLogin(InteractiveLoginEvent $event)
150+
{
151+
$user = $event->getAuthenticationToken()->getUser();
152+
$this->session->set('_locale', $user->getLocale());
153+
}
154+
}
155+
156+
Then register the listener:
157+
158+
.. configuration-block::
159+
160+
.. code-block:: yaml
161+
162+
# app/config/services.yml
163+
services:
164+
app.user_locale_listener:
165+
class: AppBundle\EventListener\UserLocaleListener
166+
tags:
167+
- { name: kernel.event_listener, event: security.interactive_login, method: onInteractiveLogin }
168+
169+
.. code-block:: xml
170+
171+
<!-- app/config/services.xml -->
172+
<service id="kernel.listener.your_listener_name" class="AppBundle\EventListener\UserLocaleListener">
173+
<tag name="kernel.event_listener" event="security.interactive_login" method="onInteractiveLogin" />
174+
</service>
175+
176+
.. code-block:: php
177+
178+
// app/config/services.php
179+
$container
180+
->register('kernel.listener.your_listener_name', 'AppBundle\EventListener\UserLocaleListener')
181+
->addTag('kernel.event_listener', array('event' => 'security.interactive_login', 'method' => 'onInteractiveLogin'))
182+
;
183+
184+
.. caution::
185+
186+
With this configuration you are all set for having the locale based on the user's
187+
locale. If however the locale changes during the session it would not be updated
188+
since with the current implementation the user locale will only be stored to the
189+
session on login. In order to update the language immediately after a user has
190+
changed his language you need to update the session variable after an update to
191+
the user entity.

0 commit comments

Comments
 (0)