@@ -107,21 +107,23 @@ method::
107107 $locale = $request->getLocale();
108108 }
109109
110- Setting the locale based on the user entity
110+ Setting the Locale based on the User Entity
111111-------------------------------------------
112112
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
117117which is logged in.
118118
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
120120want to be used as the locale for the given user. In order to achieve the wanted locale
121121configuration you can set the locale which is defined for the user to the session right
122122after the login. Fortunately you can hook into the login process and update your session
123123variable 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
125127
126128 // src/AppBundle/EventListener/UserLocaleListener.php
127129 namespace AppBundle\EventListener;
@@ -139,17 +141,22 @@ variable before the redirect to the first page. For this you need an event liste
139141 * @var Session
140142 */
141143 private $session;
144+
142145 public function __construct(Session $session)
143146 {
144147 $this->session = $session;
145148 }
149+
146150 /**
147151 * @param InteractiveLoginEvent $event
148152 */
149153 public function onInteractiveLogin(InteractiveLoginEvent $event)
150154 {
151155 $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+ }
153160 }
154161 }
155162
@@ -183,9 +190,9 @@ Then register the listener:
183190
184191 .. caution ::
185192
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