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 ba7ab6f

Browse filesBrowse files
committed
[#2662] Tweaks to new sticky locale cookbook by @wouterj
The name was changed only because 2.1 is end-of-life, and I think it's more interesting how to know how to make your locale sticky, and less important that this was the old behavior.
1 parent ae2951a commit ba7ab6f
Copy full SHA for ba7ab6f

File tree

Expand file treeCollapse file tree

3 files changed

+23
-10
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+23
-10
lines changed

‎cookbook/map.rst.inc

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

104104
* :doc:`/cookbook/request/mime_type`
105-
* (session) :doc:`/cookbook/session/simulate_locale_in_session`
105+
* (session) :doc:`/cookbook/session/locale_sticky_session`
106106

107107
* :doc:`/cookbook/routing/index`
108108

@@ -136,7 +136,7 @@
136136
* :doc:`/cookbook/session/index`
137137

138138
* :doc:`/cookbook/session/proxy_examples`
139-
* :doc:`/cookbook/session/simulate_locale_in_session`
139+
* :doc:`/cookbook/session/locale_sticky_session`
140140

141141
* **symfony1**
142142

‎cookbook/session/index.rst

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

77
proxy_examples
8-
simulate_locale_in_session
8+
locale_sticky_session

‎cookbook/session/simulate_locale_in_session.rst renamed to ‎cookbook/session/locale_sticky_session.rst

Copy file name to clipboardExpand all lines: cookbook/session/locale_sticky_session.rst
+20-7Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
.. index::
22
single: Sessions, saving locale
33

4-
Simulate old Behaviour of Saving the Locale
5-
===========================================
4+
Making the Locale "Sticky" during a User's Session
5+
==================================================
66

77
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.
8+
Since 2.1, it is stored in the Request, which means that it's not "sticky"
9+
during a user's request. In this article, you'll learn how to make the locale
10+
of a user "sticky" so that once it's set, that same locale will be used for
11+
every subsequent request.
1012

1113
Creating LocaleListener
1214
-----------------------
1315

1416
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``::
17+
register a :doc:`new event listener</cookbook/service_container/event_listener>`.
18+
The listener will look something like this. Typically, ``_locale`` is used
19+
as a routing parameter to signify the locale, though it doesn't really matter
20+
how you determine the desired locale from the request::
1821

1922
// src/Acme/LocaleBundle/EventListener/LocaleListener.php
2023
namespace Acme\LocaleBundle\EventListener;
@@ -39,9 +42,11 @@ that the parameter which handels the locale value in the request is called
3942
return;
4043
}
4144

45+
// try to see if the locale has been set as a _locale routing parameter
4246
if ($locale = $request->attributes->get('_locale')) {
4347
$request->getSession()->set('_locale', $locale);
4448
} else {
49+
// if no explicit locale has been set on this request, use one from the session
4550
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
4651
}
4752
}
@@ -88,3 +93,11 @@ Then register the listener:
8893
))
8994
->addTag('kernel.event_subscriber')
9095
;
96+
97+
That's it! Now celebrate by changing the user's locale and seeing that it's
98+
sticky throughout the request. Remember, to get the user's locale, always
99+
use the :method:`Request::getLocale<Symfony\\Component\\HttpFoundation\\Request::getLocale>`
100+
method::
101+
102+
// from a controller...
103+
$locale = $this->getRequest()->getLocale();

0 commit comments

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