@@ -106,3 +106,86 @@ method::
106
106
{
107
107
$locale = $request->getLocale();
108
108
}
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\E ventListener;
128
+
129
+ use Symfony\C omponent\H ttpFoundation\S ession\S ession;
130
+ use Symfony\C omponent\S ecurity\H ttp\E vent\I nteractiveLoginEvent;
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