Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ae2951a

Browse filesBrowse files
committed
Merge pull request #2662 from WouterJ/issue_2155
Documented how to simulate old way of saving locale
2 parents cbb304f + ff7176e commit ae2951a
Copy full SHA for ae2951a

File tree

Expand file treeCollapse file tree

3 files changed

+93
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+93
-0
lines changed

‎cookbook/map.rst.inc

Copy file name to clipboardExpand all lines: cookbook/map.rst.inc
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
* :doc:`/cookbook/request/index`
103103

104104
* :doc:`/cookbook/request/mime_type`
105+
* (session) :doc:`/cookbook/session/simulate_locale_in_session`
105106

106107
* :doc:`/cookbook/routing/index`
107108

@@ -135,6 +136,7 @@
135136
* :doc:`/cookbook/session/index`
136137

137138
* :doc:`/cookbook/session/proxy_examples`
139+
* :doc:`/cookbook/session/simulate_locale_in_session`
138140

139141
* **symfony1**
140142

‎cookbook/session/index.rst

Copy file name to clipboardExpand all lines: cookbook/session/index.rst
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ Sessions
55
:maxdepth: 2
66

77
proxy_examples
8+
simulate_locale_in_session
+90Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
.. index::
2+
single: Sessions, saving locale
3+
4+
Simulate old Behaviour of Saving the Locale
5+
===========================================
6+
7+
Prior to Symfony 2.1, the locale was stored in a session called ``_locale``.
8+
Since 2.1, it is stored in the Request. You'll learn how to simulate the old
9+
way in this article.
10+
11+
Creating LocaleListener
12+
-----------------------
13+
14+
To simulate that the locale is stored in a session, you need to create and
15+
register a new listener. The listener will look like the following, assuming
16+
that the parameter which handels the locale value in the request is called
17+
``_locale``::
18+
19+
// src/Acme/LocaleBundle/EventListener/LocaleListener.php
20+
namespace Acme\LocaleBundle\EventListener;
21+
22+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
23+
use Symfony\Component\HttpKernel\KernelEvents;
24+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
25+
26+
class LocaleListener implements EventSubscriberInterface
27+
{
28+
private $defaultLocale;
29+
30+
public function __construct($defaultLocale = 'en')
31+
{
32+
$this->defaultLocale = $defaultLocale;
33+
}
34+
35+
public function onKernelRequest(GetResponseEvent $event)
36+
{
37+
$request = $event->getRequest();
38+
if (!$request->hasPreviousSession()) {
39+
return;
40+
}
41+
42+
if ($locale = $request->attributes->get('_locale')) {
43+
$request->getSession()->set('_locale', $locale);
44+
} else {
45+
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
46+
}
47+
}
48+
49+
public static function getSubscribedEvents()
50+
{
51+
return array(
52+
// must be registered before the default Locale listener
53+
KernelEvents::REQUEST => array(array('onKernelRequest', 17)),
54+
);
55+
}
56+
}
57+
58+
Then register the listener:
59+
60+
.. configuration-block::
61+
62+
.. code-block:: yaml
63+
64+
services:
65+
acme_locale.locale_listener:
66+
class: Acme\LocaleBundle\EventListener\LocaleListener
67+
arguments: ["%kernel.default_locale%"]
68+
tags:
69+
- { name: kernel.event_subscriber }
70+
71+
.. code-block:: xml
72+
73+
<service id="acme_locale.locale_listener"
74+
class="Acme\LocaleBundle\EventListener\LocaleListener">
75+
<argument>%kernel.default_locale%</argument>
76+
77+
<tag name="kernel.event_subscriber" />
78+
</service>
79+
80+
.. code-block:: php
81+
82+
use Symfony\Component\DependencyInjection\Definition;
83+
84+
$container
85+
->setDefinition('acme_locale.locale_listener', new Definition(
86+
'Acme\LocaleBundle\EventListener\LocaleListener',
87+
array('%kernel.default_locale%')
88+
))
89+
->addTag('kernel.event_subscriber')
90+
;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.