@@ -1105,25 +1105,11 @@ Switch Locale Programmatically
11051105
11061106 The ``LocaleSwitcher `` was introduced in Symfony 6.1.
11071107
1108- Sometimes you need to change the locale of the application dynamically
1109- just to run some code. Imagine a console command that renders Twig templates
1110- of emails in different languages. You need to change the locale only to
1111- render those templates.
1108+ Sometimes you need to change the application's locale dynamically while running
1109+ some code. For example, a console command that renders email templates in
1110+ different languages. In such cases, you only need to switch the locale temporarily.
11121111
1113- .. note ::
1114-
1115- The LocaleSwitcher will apply the locale at a request level
1116- this means that it will be available only for that request. A
1117- redirect, for example, will cancel the LocaleSwitcher's effect.
1118- For a permanent locale switch between requests see https://symfony.com/doc/current/session.html#making-the-locale-sticky-during-a-user-s-session.
1119-
1120- The ``LocaleSwitcher `` class allows you to change at once the locale
1121- of:
1122-
1123- * All the services that are tagged with ``kernel.locale_aware ``;
1124- * ``\Locale::setDefault() ``;
1125- * If the ``RequestContext `` service is available, the ``_locale ``
1126- parameter (so urls are generated with the new locale)::
1112+ The ``LocaleSwitcher `` class allows you to do that::
11271113
11281114 use Symfony\Component\Translation\LocaleSwitcher;
11291115
@@ -1136,28 +1122,23 @@ of:
11361122
11371123 public function someMethod(): void
11381124 {
1139- // you can get the current application locale like this:
11401125 $currentLocale = $this->localeSwitcher->getLocale();
11411126
1142- // you can set the locale for the entire application like this:
1143- // (from now on, the application will use 'fr' (French) as the
1144- // locale; including the default locale used to translate Twig templates)
1127+ // set the application locale programmatically to 'fr' (French):
1128+ // this affects translation, URL generation, etc.
11451129 $this->localeSwitcher->setLocale('fr');
11461130
1147- // reset the current locale of your application to the configured default locale
1148- // in config/packages/translation.yaml, by option 'default_locale'
1131+ // reset the locale to the default one configured via the
1132+ // 'default_locale' option in config/packages/translation.yaml
11491133 $this->localeSwitcher->reset();
11501134
1151- // you can also run some code with a certain locale, without
1135+ // run some code with a specific locale, temporarily , without
11521136 // changing the locale for the rest of the application
11531137 $this->localeSwitcher->runWithLocale('es', function() {
1154-
1155- // e.g. render here some Twig templates using 'es' (Spanish) locale
1156-
1138+ // e.g. render templates, send emails, etc. using the 'es' (Spanish) locale
11571139 });
11581140
1159- // you can optionally declare an argument in your callback to receive the
1160- // injected locale
1141+ // optionally, receive the current locale as an argument:
11611142 $this->localeSwitcher->runWithLocale('es', function(string $locale) {
11621143
11631144 // here, the $locale argument will be set to 'es'
@@ -1175,6 +1156,20 @@ of:
11751156 :method: `Symfony\\ Component\\ Translation\\ LocaleSwitcher::runWithLocale `
11761157 method was introduced in Symfony 6.4.
11771158
1159+ The ``LocaleSwitcher `` class changes the locale of:
1160+
1161+ * All services tagged with ``kernel.locale_aware ``;
1162+ * The default locale set via ``\Locale::setDefault() ``;
1163+ * The ``_locale `` parameter of the ``RequestContext `` service (if available),
1164+ so generated URLs reflect the new locale.
1165+
1166+ .. note ::
1167+
1168+ The LocaleSwitcher applies the new locale only for the current request,
1169+ and its effect is lost on subsequent requests, such as after a redirect.
1170+
1171+ See :ref: `how to make the locale persist across requests <locale-sticky-session >`.
1172+
11781173When using :ref: `autowiring <services-autowire >`, type-hint any controller or
11791174service argument with the :class: `Symfony\\ Component\\ Translation\\ LocaleSwitcher `
11801175class to inject the locale switcher service. Otherwise, configure your services
0 commit comments