8000 Rebase of #4989 by weaverryan · Pull Request #5099 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

Rebase of #4989 #5099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove useless setLocale() call and add code block with locale setter
  • Loading branch information
solazs authored and weaverryan committed Mar 20, 2015
commit 65e39b125abd2b638783e61948badd7bd22c0532
31 changes: 22 additions & 9 deletions book/translation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -427,22 +427,35 @@ via the ``request`` object::
public function indexAction(Request $request)
{
$locale = $request->getLocale();

$request->setLocale('en_US');
}

To store the user's locale in the session you may want to create a custom event listener and set the locale there::

public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if (!$request->hasPreviousSession()) {
return;
}

// try to see if the locale has been set as a _locale routing parameter
if ($locale = $request->attributes->get('_locale')) {
$request->getSession()->set('_locale', $locale);
} else {
// if no explicit locale has been set on this request, use one from the session
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
}
}

Read :doc:`/cookbook/session/locale_sticky_session` for more on the topic.

.. note::

Setting the locale using the ``$request->setLocale()`` method won't affect
rendering in the same action. The translator locale is set during the
``kernel.request`` event. Either set the locale before the listener is called
(e.g. in a custom listener) or use the ``setLocale()`` method of the
``translator`` service.

.. tip::

Read :doc:`/cookbook/session/locale_sticky_session` to learn how to store
the user's locale in the session.
(e.g. in a custom listener described above) or use the ``setLocale()`` method
of the ``translator`` service.

.. index::
single: Translations; Fallback and default locale
Expand Down
0