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 all commits
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
28 changes: 21 additions & 7 deletions book/translation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -427,17 +427,28 @@ via the ``request`` object::
public function indexAction(Request $request)
{
$locale = $request->getLocale();

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

.. tip::
To set the user's locale, you may want to create a custom event listener
so that it's set before any other parts of the system (i.e. the translator)
need it::

Read :doc:`/cookbook/session/locale_sticky_session` to learn how to store
the user's locale in the session.
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();

.. index::
single: Translations; Fallback and default locale
// some logic to determine the $locale
$request->getSession()->set('_locale', $locale);
}

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

.. note::

Setting the locale using ``$request->setLocale()`` in the controller
is too late to affect the translator. Either set the locale via a listener
(like above), the URL (see next) or call ``setLocale()`` directly on
the ``translator`` service.

See the :ref:`book-translation-locale-url` section below about setting the
locale via routing.
Expand Down Expand Up @@ -518,6 +529,9 @@ in your application.
Read :doc:`/cookbook/routing/service_container_parameters` to learn how to
avoid hardcoding the ``_locale`` requirement in all your routes.

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

Setting a default Locale
~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
0