Skip to content

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

Merged
merged 4 commits into from
Mar 13, 2015
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions cookbook/session/locale_sticky_session.rst
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,86 @@ method::
{
$locale = $request->getLocale();
}

Setting the locale based on the user entity
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Setting the Locale based on the User Entity"

-------------------------------------------

You might want to improve even further and want to define the locale based on
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original:

You might want to improve even further and want to define the locale based on
the user entity of the logged in user.

Proposed rewording:

You might want to improve this technique even further and define the locale based on
the user entity of the logged in user.

the user entity of the logged in user. However since the `LocaleListener` is called
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor syntax error: in RST docs, the code is wrapped with two backticks.

before the `FirewallListener`, which is responsible for handling authentication and
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you have to use double backticks in reStructed Text (same in the rest of your changes)

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably spell entity lowercased here.

want to be used as the locale for the given user. In order to achieve the wanted locale
Copy link
Member

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:

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.

configuration you can set the locale which is defined for the user to the session right
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[...] configuration, you [...]

after the login. Fortunately you can hook into the login process and update your session
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fortunately, you [...]

variable before the redirect to the first page. For this you need an event listener for the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove "variable" here, but right something like "[...] and update the user's session before [...]"

`security.interactive_login` event.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the block of code is PHP, you can omit the .. code-block:: php directive, but only if the above paragraph ends with a double colon : instead of a dot .

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in conclusion: add .. code-block:: php :)


// src/AppBundle/EventListener/UserLocaleListener.php
namespace AppBundle\EventListener;

use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;

/**
* Stores the locale of the user in the session after the
* login. This can be used by the LocaleListener afterwards.
*/
class UserLocaleListener
{
/**
* @var Session
*/
private $session;
public function __construct(Session $session)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably we need a blank line between the property and the constructor.

{
$this->session = $session;
}
/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here we probably need another blank line.

* @param InteractiveLoginEvent $event
*/
public function onInteractiveLogin(InteractiveLoginEvent $event)
{
$user = $event->getAuthenticationToken()->getUser();
$this->session->set('_locale', $user->getLocale());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can $user->getLocale() return a non-valid locale value? What would happen in that case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, adding locale only if not null. The default locale will only be used of there is no _locale defined. The logic comes from the LocaleListener.

}
}

Then register the listener:

.. configuration-block::

.. code-block:: yaml

# app/config/services.yml
services:
app.user_locale_listener:
class: AppBundle\EventListener\UserLocaleListener
tags:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you forget to inject the session here

    ... 
    arguments: [@session]

- { name: kernel.event_listener, event: security.interactive_login, method: onInteractiveLogin }

.. 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>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use a complete XML example here:

<!-- app/config/config.xml -->
<?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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be app.user_locale_listener

->addTag('kernel.event_listener', array('event' => 'security.interactive_login', 'method' => 'onInteractiveLogin'))
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The contents of the caution admonition need to be left-indented by 4 spaces.

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 his language you need to update the session variable after an update to
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We try to avoid non-gender-neutral language as much as possible. We probably can replace changed his language by changed their language, where their is used as the special singular they.

the user entity.