Commit
…class The locale management does not require sessions anymore. In the Symfony2 spirit, the locale should be part of your URLs. If this is the case (via the special _locale request attribute), Symfony will store it in the request (getLocale()). This feature is now also configurable/replaceable at will as everything is now managed by the new LocaleListener event listener. How to upgrade: The default locale configuration has been moved from session to the main configuration: Before: framework: session: default_locale: en After: framework: default_locale: en Whenever you want to get the current locale, call getLocale() on the request (was on the session before).
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\EventListener; | ||
|
||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
use Symfony\Component\Routing\RouterInterface; | ||
|
||
/** | ||
* Initializes the locale based on the current request. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
class LocaleListener | ||
{ | ||
private $router; | ||
private $defaultLocale; | ||
|
||
public function __construct($defaultLocale = 'en', RouterInterface $router = null) | ||
{ | ||
$this->defaultLocale = $defaultLocale; | ||
$this->router = $router; | ||
} | ||
|
||
public function onEarlyKernelRequest(GetResponseEvent $event) | ||
{ | ||
$request = $event->getRequest(); | ||
if ($request->hasPreviousSession()) { | ||
This comment has been minimized.
Sorry, something went wrong.
kriswallsmith
Contributor
|
||
$request->setDefaultLocale($request->getSession()->get('_locale', $this->defaultLocale)); | ||
} else { | ||
$request->setDefaultLocale($this->defaultLocale); | ||
} | ||
} | ||
|
||
public function onKernelRequest(GetResponseEvent $event) | ||
{ | ||
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) { | ||
return; | ||
} | ||
|
||
$request = $event->getRequest(); | ||
if ($locale = $request->attributes->get('_locale')) { | ||
$request->setLocale($locale); | ||
|
||
if ($request->hasPreviousSession()) { | ||
$request->getSession()->set('_locale', $request->getLocale()); | ||
} | ||
} | ||
|
||
if (null !== $this->router) { | ||
$this->router->getContext()->setParameter('_locale', $request->getLocale()); | ||
} | ||
} | ||
} |
2 comments
on commit 74bc699
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.
The Request doesn't feel like the correct object, a container-scoped context holder would be a better fit imo.
This change has the side-effect that users now have to think about the scope of their services if they need the locale. They cannot just change the dependency from the session to the request, doing that will likely lead to a scope error, and cause some wtfs.
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.
maybe the session should still have the locale setter/getter for BC to keep upgrading easy?
I currently do
app.session.locale
which also works fine btw