@@ -1177,18 +1177,11 @@ checks translation resources for several locales:
1177
1177
Switch Locale Programmatically
1178
1178
------------------------------
1179
1179
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.
1184
1183
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::
1192
1185
1193
1186
use Symfony\Component\Translation\LocaleSwitcher;
1194
1187
@@ -1201,28 +1194,23 @@ of:
1201
1194
1202
1195
public function someMethod(): void
1203
1196
{
1204
- // you can get the current application locale like this:
1205
1197
$currentLocale = $this->localeSwitcher->getLocale();
1206
1198
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.
1210
1201
$this->localeSwitcher->setLocale('fr');
1211
1202
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
1214
1205
$this->localeSwitcher->reset();
1215
1206
1216
- // you can also run some code with a certain locale, without
1207
+ // run some code with a specific locale, temporarily , without
1217
1208
// changing the locale for the rest of the application
1218
1209
$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
1222
1211
});
1223
1212
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:
1226
1214
$this->localeSwitcher->runWithLocale('es', function(string $locale) {
1227
1215
1228
1216
// here, the $locale argument will be set to 'es'
@@ -1233,6 +1221,20 @@ of:
1233
1221
}
1234
1222
}
1235
1223
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
+
1236
1238
When using :ref: `autowiring <services-autowire >`, type-hint any controller or
1237
1239
service argument with the :class: `Symfony\\ Component\\ Translation\\ LocaleSwitcher `
1238
1240
class to inject the locale switcher service. Otherwise, configure your services
0 commit comments