From ff7176e6ed8de6f34b6f39665252cad6624044e8 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Sun, 26 May 2013 16:17:04 +0200 Subject: [PATCH] Added article on how to simulate saving locale in session --- cookbook/map.rst.inc | 2 + cookbook/session/index.rst | 1 + .../session/simulate_locale_in_session.rst | 90 +++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 cookbook/session/simulate_locale_in_session.rst diff --git a/cookbook/map.rst.inc b/cookbook/map.rst.inc index 67ccf2255d3..cc476210dbd 100644 --- a/cookbook/map.rst.inc +++ b/cookbook/map.rst.inc @@ -102,6 +102,7 @@ * :doc:`/cookbook/request/index` * :doc:`/cookbook/request/mime_type` + * (session) :doc:`/cookbook/session/simulate_locale_in_session` * :doc:`/cookbook/routing/index` @@ -135,6 +136,7 @@ * :doc:`/cookbook/session/index` * :doc:`/cookbook/session/proxy_examples` + * :doc:`/cookbook/session/simulate_locale_in_session` * **symfony1** diff --git a/cookbook/session/index.rst b/cookbook/session/index.rst index 687148dcd10..e2289b7cd66 100644 --- a/cookbook/session/index.rst +++ b/cookbook/session/index.rst @@ -5,3 +5,4 @@ Sessions :maxdepth: 2 proxy_examples + simulate_locale_in_session diff --git a/cookbook/session/simulate_locale_in_session.rst b/cookbook/session/simulate_locale_in_session.rst new file mode 100644 index 00000000000..68e1c44878e --- /dev/null +++ b/cookbook/session/simulate_locale_in_session.rst @@ -0,0 +1,90 @@ +.. index:: + single: Sessions, saving locale + +Simulate old Behaviour of Saving the Locale +=========================================== + +Prior to Symfony 2.1, the locale was stored in a session called ``_locale``. +Since 2.1, it is stored in the Request. You'll learn how to simulate the old +way in this article. + +Creating LocaleListener +----------------------- + +To simulate that the locale is stored in a session, you need to create and +register a new listener. The listener will look like the following, assuming +that the parameter which handels the locale value in the request is called +``_locale``:: + + // src/Acme/LocaleBundle/EventListener/LocaleListener.php + namespace Acme\LocaleBundle\EventListener; + + use Symfony\Component\HttpKernel\Event\GetResponseEvent; + use Symfony\Component\HttpKernel\KernelEvents; + use Symfony\Component\EventDispatcher\EventSubscriberInterface; + + class LocaleListener implements EventSubscriberInterface + { + private $defaultLocale; + + public function __construct($defaultLocale = 'en') + { + $this->defaultLocale = $defaultLocale; + } + + public function onKernelRequest(GetResponseEvent $event) + { + $request = $event->getRequest(); + if (!$request->hasPreviousSession()) { + return; + } + + if ($locale = $request->attributes->get('_locale')) { + $request->getSession()->set('_locale', $locale); + } else { + $request->setLocale($request->getSession()->get('_locale', $this->defaultLocale)); + } + } + + public static function getSubscribedEvents() + { + return array( + // must be registered before the default Locale listener + KernelEvents::REQUEST => array(array('onKernelRequest', 17)), + ); + } + } + +Then register the listener: + +.. configuration-block:: + + .. code-block:: yaml + + services: + acme_locale.locale_listener: + class: Acme\LocaleBundle\EventListener\LocaleListener + arguments: ["%kernel.default_locale%"] + tags: + - { name: kernel.event_subscriber } + + .. code-block:: xml + + + %kernel.default_locale% + + + + + .. code-block:: php + + use Symfony\Component\DependencyInjection\Definition; + + $container + ->setDefinition('acme_locale.locale_listener', new Definition( + 'Acme\LocaleBundle\EventListener\LocaleListener', + array('%kernel.default_locale%') + )) + ->addTag('kernel.event_subscriber') + ;