@@ -107,21 +107,23 @@ method::
107
107
$locale = $request->getLocale();
108
108
}
109
109
110
- Setting the locale based on the user entity
110
+ Setting the Locale based on the User Entity
111
111
-------------------------------------------
112
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
113
+ You might want to improve this technique even further and 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
117
which is logged in.
118
118
119
- First lets pretend you have defined a property locale in your User Entity which you
119
+ First lets pretend you have defined a property locale in your user entity which you
120
120
want to be used as the locale for the given user. In order to achieve the wanted locale
121
121
configuration you can set the locale which is defined for the user to the session right
122
122
after the login. Fortunately you can hook into the login process and update your session
123
123
variable before the redirect to the first page. For this you need an event listener for the
124
- `security.interactive_login ` event.
124
+ ``security.interactive_login `` event.
125
+
126
+ .. code-block :: php
125
127
126
128
// src/AppBundle/EventListener/UserLocaleListener.php
127
129
namespace AppBundle\EventListener;
@@ -139,17 +141,22 @@ variable before the redirect to the first page. For this you need an event liste
139
141
* @var Session
140
142
*/
141
143
private $session;
144
+
142
145
public function __construct(Session $session)
143
146
{
144
147
$this->session = $session;
145
148
}
149
+
146
150
/**
147
151
* @param InteractiveLoginEvent $event
148
152
*/
149
153
public function onInteractiveLogin(InteractiveLoginEvent $event)
150
154
{
151
155
$user = $event->getAuthenticationToken()->getUser();
152
- $this->session->set('_locale', $user->getLocale());
156
+
157
+ if (null !== $user->getLocale()) {
158
+ $this->session->set('_locale', $user->getLocale());
159
+ }
153
160
}
154
161
}
155
162
@@ -183,9 +190,9 @@ Then register the listener:
183
190
184
191
.. caution ::
185
192
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.
193
+ With this configuration you are all set for having the locale based on the user's
194
+ locale. If however the locale changes during the session it would not be updated
195
+ since with the current implementation the user locale will only be stored to the
196
+ session on login. In order to update the language immediately after a user has
197
+ changed their language you need to update the session variable after an update to
198
+ the user entity.
0 commit comments