-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Added chapter about the locale based on the user entity #4875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,20 +107,20 @@ method:: | |
$locale = $request->getLocale(); | ||
} | ||
|
||
Setting the Locale based on the User Entity | ||
------------------------------------------- | ||
Setting the Locale Based on the User's Preferences | ||
-------------------------------------------------- | ||
|
||
You might want to improve this technique even further and define the locale based on | ||
the user entity of the logged in user. However since the ``LocaleListener`` is called | ||
the user entity of the logged in user. However, since the ``LocaleListener`` is called | ||
before the ``FirewallListener``, which is responsible for handling authentication and | ||
is setting the user token into the ``TokenStorage``, you have no access to the user | ||
which is logged in. | ||
|
||
First lets pretend you have defined a property locale in your user entity which you | ||
Let's pretend you have defined a property "locale" in your user entity which you | ||
want to be used as the locale for the given user. In order to achieve the wanted locale | ||
configuration you can set the locale which is defined for the user to the session right | ||
after the login. Fortunately you can hook into the login process and update your session | ||
variable before the redirect to the first page. For this you need an event listener for the | ||
configuration, you can set the locale which is defined for the user to the session right | ||
after the login. Fortunately, you can hook into the login process and update the user's | ||
session before the redirect to the first page. For this you need an event listener for the | ||
``security.interactive_login`` event. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could end this sentence with a colon. |
||
|
||
.. code-block:: php | ||
|
@@ -175,24 +175,37 @@ Then register the listener: | |
|
||
.. code-block:: xml | ||
|
||
<!-- app/config/services.xml --> | ||
<service id="kernel.listener.your_listener_name" class="AppBundle\EventListener\UserLocaleListener"> | ||
<tag name="kernel.event_listener" event="security.interactive_login" method="onInteractiveLogin" /> | ||
</service> | ||
<!-- app/config/config.xml --> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services | ||
http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<service id="app.user_locale_listener" | ||
class="AppBundle\EventListener\UserLocaleListener"> | ||
|
||
<tag name="kernel.event_listener" | ||
event="security.interactive_login" | ||
method="onInteractiveLogin" /> | ||
</service> | ||
</services> | ||
</container> | ||
|
||
.. code-block:: php | ||
|
||
// app/config/services.php | ||
$container | ||
->register('kernel.listener.your_listener_name', 'AppBundle\EventListener\UserLocaleListener') | ||
->register('app.user_locale_listener', 'AppBundle\EventListener\UserLocaleListener') | ||
->addTag('kernel.event_listener', array('event' => 'security.interactive_login', 'method' => 'onInteractiveLogin')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would wrap this to avoid horizontal scrollbars. |
||
; | ||
|
||
.. caution:: | ||
|
||
With this configuration you are all set for having the locale based on the user's | ||
locale. If however the locale changes during the session it would not be updated | ||
locale. If however the locale changes during the session, it would not be updated | ||
since with the current implementation the user locale will only be stored to the | ||
session on login. In order to update the language immediately after a user has | ||
changed their language you need to update the session variable after an update to | ||
changed their language, you need to update the session after an update to | ||
the user entity. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find this phrase too long and a bit confusing. Maybe we should reword it or split it: