@@ -107,20 +107,20 @@ method::
107
107
$locale = $request->getLocale();
108
108
}
109
109
110
- Setting the Locale based on the User Entity
111
- -------------------------------------------
110
+ Setting the Locale Based on the User's Preferences
111
+ --------------------------------------------------
112
112
113
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
114
+ the user entity of the logged in user. However, since the ``LocaleListener `` is called
115
115
before the ``FirewallListener ``, which is responsible for handling authentication and
116
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
+ Let's 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
- 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
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 the user's
123
+ session before the redirect to the first page. For this you need an event listener for the
124
124
``security.interactive_login `` event.
125
125
126
126
.. code-block :: php
@@ -175,24 +175,37 @@ Then register the listener:
175
175
176
176
.. code-block :: xml
177
177
178
- <!-- app/config/services.xml -->
179
- <service id =" kernel.listener.your_listener_name" class =" AppBundle\EventListener\UserLocaleListener" >
180
- <tag name =" kernel.event_listener" event =" security.interactive_login" method =" onInteractiveLogin" />
181
- </service >
178
+ <!-- app/config/config.xml -->
179
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
180
+ <container xmlns =" http://symfony.com/schema/dic/services"
181
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
182
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
183
+ http://symfony.com/schema/dic/services/services-1.0.xsd" >
184
+
185
+ <services >
186
+ <service id =" app.user_locale_listener"
187
+ class =" AppBundle\EventListener\UserLocaleListener" >
188
+
189
+ <tag name =" kernel.event_listener"
190
+ event =" security.interactive_login"
191
+ method =" onInteractiveLogin" />
192
+ </service >
193
+ </services >
194
+ </container >
182
195
183
196
.. code-block :: php
184
197
185
198
// app/config/services.php
186
199
$container
187
- ->register('kernel.listener.your_listener_name ', 'AppBundle\EventListener\UserLocaleListener')
200
+ ->register('app.user_locale_listener ', 'AppBundle\EventListener\UserLocaleListener')
188
201
->addTag('kernel.event_listener', array('event' => 'security.interactive_login', 'method' => 'onInteractiveLogin'))
189
202
;
190
203
191
204
.. caution ::
192
205
193
206
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
207
+ locale. If however the locale changes during the session, it would not be updated
195
208
since with the current implementation the user locale will only be stored to the
196
209
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
210
+ changed their language, you need to update the session after an update to
198
211
the user entity.
0 commit comments