Skip to content

Commit 0fc72a8

Browse files
committed
Updated as per feedback
1 parent 6e986e1 commit 0fc72a8

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

cookbook/session/locale_sticky_session.rst

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,20 @@ method::
107107
$locale = $request->getLocale();
108108
}
109109

110-
Setting the Locale based on the User Entity
111-
-------------------------------------------
110+
Setting the Locale Based on the User's Preferences
111+
--------------------------------------------------
112112

113113
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
115115
before the ``FirewallListener``, which is responsible for handling authentication and
116116
is setting the user token into the ``TokenStorage``, you have no access to the user
117117
which is logged in.
118118

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
120120
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
124124
``security.interactive_login`` event.
125125

126126
.. code-block:: php
@@ -175,24 +175,37 @@ Then register the listener:
175175
176176
.. code-block:: xml
177177
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>
182195
183196
.. code-block:: php
184197
185198
// app/config/services.php
186199
$container
187-
->register('kernel.listener.your_listener_name', 'AppBundle\EventListener\UserLocaleListener')
200+
->register('app.user_locale_listener', 'AppBundle\EventListener\UserLocaleListener')
188201
->addTag('kernel.event_listener', array('event' => 'security.interactive_login', 'method' => 'onInteractiveLogin'))
189202
;
190203
191204
.. caution::
192205

193206
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
195208
since with the current implementation the user locale will only be stored to the
196209
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
198211
the user entity.

0 commit comments

Comments
 (0)