@@ -1177,18 +1177,11 @@ checks translation resources for several locales:
11771177Switch Locale Programmatically
11781178------------------------------
11791179
1180- Sometimes you need to change the locale of the application dynamically
1181- just to run some code. Imagine a console command that renders Twig templates
1182- of emails in different languages. You need to change the locale only to
1183- render those templates.
1180+ Sometimes you need to change the application's locale dynamically while running
1181+ some code. For example, a console command that renders email templates in
1182+ different languages. In such cases, you only need to switch the locale temporarily.
11841183
1185- The ``LocaleSwitcher `` class allows you to change at once the locale
1186- of:
1187-
1188- * All the services that are tagged with ``kernel.locale_aware ``;
1189- * ``\Locale::setDefault() ``;
1190- * If the ``RequestContext `` service is available, the ``_locale ``
1191- parameter (so urls are generated with the new locale)::
1184+ The ``LocaleSwitcher `` class allows you to do that::
11921185
11931186 use Symfony\Component\Translation\LocaleSwitcher;
11941187
@@ -1201,28 +1194,23 @@ of:
12011194
12021195 public function someMethod(): void
12031196 {
1204- // you can get the current application locale like this:
12051197 $currentLocale = $this->localeSwitcher->getLocale();
12061198
1207- // you can set the locale for the entire application like this:
1208- // (from now on, the application will use 'fr' (French) as the
1209- // locale; including the default locale used to translate Twig templates)
1199+ // set the application locale programmatically to 'fr' (French):
1200+ // this affects translation, URL generation, etc.
12101201 $this->localeSwitcher->setLocale('fr');
12111202
1212- // reset the current locale of your application to the configured default locale
1213- // in config/packages/translation.yaml, by option 'default_locale'
1203+ // reset the locale to the default one configured via the
1204+ // 'default_locale' option in config/packages/translation.yaml
12141205 $this->localeSwitcher->reset();
12151206
1216- // you can also run some code with a certain locale, without
1207+ // run some code with a specific locale, temporarily , without
12171208 // changing the locale for the rest of the application
12181209 $this->localeSwitcher->runWithLocale('es', function() {
1219-
1220- // e.g. render here some Twig templates using 'es' (Spanish) locale
1221-
1210+ // e.g. render templates, send emails, etc. using the 'es' (Spanish) locale
12221211 });
12231212
1224- // you can optionally declare an argument in your callback to receive the
1225- // injected locale
1213+ // optionally, receive the current locale as an argument:
12261214 $this->localeSwitcher->runWithLocale('es', function(string $locale) {
12271215
12281216 // here, the $locale argument will be set to 'es'
@@ -1233,6 +1221,20 @@ of:
12331221 }
12341222 }
12351223
1224+ The ``LocaleSwitcher `` class changes the locale of:
1225+
1226+ * All services tagged with ``kernel.locale_aware ``;
1227+ * The default locale set via ``\Locale::setDefault() ``;
1228+ * The ``_locale `` parameter of the ``RequestContext `` service (if available),
1229+ so generated URLs reflect the new locale.
1230+
1231+ .. note ::
1232+
1233+ The LocaleSwitcher applies the new locale only for the current request,
1234+ and its effect is lost on subsequent requests, such as after a redirect.
1235+
1236+ See :ref: `how to make the locale persist across requests <locale-sticky-session >`.
1237+
12361238When using :ref: `autowiring <services-autowire >`, type-hint any controller or
12371239service argument with the :class: `Symfony\\ Component\\ Translation\\ LocaleSwitcher `
12381240class to inject the locale switcher service. Otherwise, configure your services
0 commit comments