From 3b136c8ab6d3662930e55290a6e4e73ddbff4b04 Mon Sep 17 00:00:00 2001 From: solazs Date: Wed, 11 Feb 2015 16:43:56 +0100 Subject: [PATCH 1/6] Update translation.rst Clarify that changing the locale in the controller would not affect template rendering as the translator reads the request locale during the kernel.request event. --- book/translation.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/book/translation.rst b/book/translation.rst index cdaca6a1753..a5e009d25e9 100644 --- a/book/translation.rst +++ b/book/translation.rst @@ -424,6 +424,14 @@ via the ``request`` object:: $request->setLocale('en_US'); } + + +.. note:: + + Setting the locale using ``$request->setLocale()`` won't affect rendering + in the same action as the translator reads the request locale during the + kernel.request event, so changing it here would be too late. To manually + change translation locale in the controller use ``$this->get('translator')->setLocale()``. .. tip:: From 79f5865aca61414f128ef2ebc077bbb6e192b7cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Solt=C3=A9sz=20Bal=C3=A1zs?= Date: Sun, 15 Feb 2015 16:01:19 +0100 Subject: [PATCH 2/6] Review note about setting the translator locale in a controller. --- book/translation.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/book/translation.rst b/book/translation.rst index a5e009d25e9..6131ad5dc7c 100644 --- a/book/translation.rst +++ b/book/translation.rst @@ -425,13 +425,13 @@ via the ``request`` object:: $request->setLocale('en_US'); } - .. note:: - Setting the locale using ``$request->setLocale()`` won't affect rendering - in the same action as the translator reads the request locale during the - kernel.request event, so changing it here would be too late. To manually - change translation locale in the controller use ``$this->get('translator')->setLocale()``. + 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:: From 43b209828365d18a7fd69097520c2d740ed16c94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Solt=C3=A9sz=20Bal=C3=A1zs?= Date: Fri, 20 Feb 2015 02:26:32 +0100 Subject: [PATCH 3/6] Finaly touches on translation locale setting note --- book/translation.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/book/translation.rst b/book/translation.rst index 6131ad5dc7c..36ed4dfe7d4 100644 --- a/book/translation.rst +++ b/book/translation.rst @@ -429,7 +429,7 @@ via the ``request`` object:: 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 + ``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. From 1ad812270362141419dc23868ae79845234365bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Solt=C3=A9sz=20Bal=C3=A1zs?= Date: Fri, 6 Mar 2015 16:24:45 +0100 Subject: [PATCH 4/6] Remove useless setLocale() call and add code block with locale setter --- book/translation.rst | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/book/translation.rst b/book/translation.rst index 36ed4dfe7d4..2bd5bcd06f7 100644 --- a/book/translation.rst +++ b/book/translation.rst @@ -421,22 +421,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 From 7281c532249acbcf693a6df7e8b14520ccf82011 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Thu, 19 Mar 2015 21:13:44 -0400 Subject: [PATCH 5/6] [#4989] Language tweaks and making the example simpler --- book/translation.rst | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/book/translation.rst b/book/translation.rst index 2bd5bcd06f7..e07eeec6198 100644 --- a/book/translation.rst +++ b/book/translation.rst @@ -422,34 +422,27 @@ via the ``request`` object:: { $locale = $request->getLocale(); } - -To store the user's locale in the session you may want to create a custom event listener and set the locale there:: + +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:: 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)); - } + // 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 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 described above) or use the ``setLocale()`` method - of the ``translator`` service. + 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. .. index:: single: Translations; Fallback and default locale From 006303d11b3e6c71417c94898930ba3ae687f40c Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Thu, 19 Mar 2015 21:14:10 -0400 Subject: [PATCH 6/6] Moving index down to correct section --- book/translation.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/book/translation.rst b/book/translation.rst index e07eeec6198..985c088cf53 100644 --- a/book/translation.rst +++ b/book/translation.rst @@ -444,9 +444,6 @@ Read :doc:`/cookbook/session/locale_sticky_session` for more on the topic. (like above), the URL (see next) or call ``setLocale()`` directly on the ``translator`` service. -.. index:: - single: Translations; Fallback and default locale - See the :ref:`book-translation-locale-url` section below about setting the locale via routing. @@ -526,6 +523,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 ~~~~~~~~~~~~~~~~~~~~~~~~