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 1ad8122

Browse filesBrowse files
solazsweaverryan
authored andcommitted
Remove useless setLocale() call and add code block with locale setter
1 parent 43b2098 commit 1ad8122
Copy full SHA for 1ad8122

File tree

Expand file treeCollapse file tree

1 file changed

+22
-9
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+22
-9
lines changed

‎book/translation.rst

Copy file name to clipboardExpand all lines: book/translation.rst
+22-9Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -421,22 +421,35 @@ via the ``request`` object::
421421
public function indexAction(Request $request)
422422
{
423423
$locale = $request->getLocale();
424-
425-
$request->setLocale('en_US');
426424
}
427425
426+
To store the user's locale in the session you may want to create a custom event listener and set the locale there::
427+
428+
public function onKernelRequest(GetResponseEvent $event)
429+
{
430+
$request = $event->getRequest();
431+
if (!$request->hasPreviousSession()) {
432+
return;
433+
}
434+
435+
// try to see if the locale has been set as a _locale routing parameter
436+
if ($locale = $request->attributes->get('_locale')) {
437+
$request->getSession()->set('_locale', $locale);
438+
} else {
439+
// if no explicit locale has been set on this request, use one from the session
440+
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
441+
}
442+
}
443+
444+
Read :doc:`/cookbook/session/locale_sticky_session` for more on the topic.
445+
428446
.. note::
429447

430448
Setting the locale using the ``$request->setLocale()`` method won't affect
431449
rendering in the same action. The translator locale is set during the
432450
``kernel.request`` event. Either set the locale before the listener is called
433-
(e.g. in a custom listener) or use the ``setLocale()`` method of the
434-
``translator`` service.
435-
436-
.. tip::
437-
438-
Read :doc:`/cookbook/session/locale_sticky_session` to learn how to store
439-
the user's locale in the session.
451+
(e.g. in a custom listener described above) or use the ``setLocale()`` method
452+
of the ``translator`` service.
440453

441454
.. index::
442455
single: Translations; Fallback and default locale

0 commit comments

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