@@ -1099,18 +1099,11 @@ checks translation resources for several locales:
1099
1099
Switch Locale Programmatically
1100
1100
------------------------------
1101
1101
1102
- Sometimes you need to change the locale of the application dynamically
1103
- just to run some code. Imagine a console command that renders Twig templates
1104
- of emails in different languages. You need to change the locale only to
1105
- render those templates.
1102
+ Sometimes you need to change the application's locale dynamically while running
1103
+ some code. For example, a console command that renders email templates in
1104
+ different languages. In such cases, you only need to switch the locale temporarily.
1106
1105
1107
- The ``LocaleSwitcher `` class allows you to change at once the locale
1108
- of:
1109
-
1110
- * All the services that are tagged with ``kernel.locale_aware ``;
1111
- * ``\Locale::setDefault() ``;
1112
- * If the ``RequestContext `` service is available, the ``_locale ``
1113
- parameter (so urls are generated with the new locale)::
1106
+ The ``LocaleSwitcher `` class allows you to do that::
1114
1107
1115
1108
use Symfony\Component\Translation\LocaleSwitcher;
1116
1109
@@ -1123,28 +1116,23 @@ of:
1123
1116
1124
1117
public function someMethod(): void
1125
1118
{
1126
- // you can get the current application locale like this:
1127
1119
$currentLocale = $this->localeSwitcher->getLocale();
1128
1120
1129
- // you can set the locale for the entire application like this:
1130
- // (from now on, the application will use 'fr' (French) as the
1131
- // locale; including the default locale used to translate Twig templates)
1121
+ // set the application locale programmatically to 'fr' (French):
1122
+ // this affects translation, URL generation, etc.
1132
1123
$this->localeSwitcher->setLocale('fr');
1133
1124
1134
- // reset the current locale of your application to the configured default locale
1135
- // in config/packages/translation.yaml, by option 'default_locale'
1125
+ // reset the locale to the default one configured via the
1126
+ // 'default_locale' option in config/packages/translation.yaml
1136
1127
$this->localeSwitcher->reset();
1137
1128
1138
- // you can also run some code with a certain locale, without
1129
+ // run some code with a specific locale, temporarily , without
1139
1130
// changing the locale for the rest of the application
1140
1131
$this->localeSwitcher->runWithLocale('es', function() {
1141
-
1142
- // e.g. render here some Twig templates using 'es' (Spanish) locale
1143
-
1132
+ // e.g. render templates, send emails, etc. using the 'es' (Spanish) locale
1144
1133
});
1145
1134
1146
- // you can optionally declare an argument in your callback to receive the
1147
- // injected locale
1135
+ // optionally, receive the current locale as an argument:
1148
1136
$this->localeSwitcher->runWithLocale('es', function(string $locale) {
1149
1137
1150
1138
// here, the $locale argument will be set to 'es'
@@ -1155,6 +1143,20 @@ of:
1155
1143
}
1156
1144
}
1157
1145
1146
+ The ``LocaleSwitcher `` class changes the locale of:
1147
+
1148
+ * All services tagged with ``kernel.locale_aware ``;
1149
+ * The default locale set via ``\Locale::setDefault() ``;
1150
+ * The ``_locale `` parameter of the ``RequestContext `` service (if available),
1151
+ so generated URLs reflect the new locale.
1152
+
1153
+ .. note ::
1154
+
1155
+ The LocaleSwitcher applies the new locale only for the current request,
1156
+ and its effect is lost on subsequent requests, such as after a redirect.
1157
+
1158
+ See :ref: `how to make the locale persist across requests <locale-sticky-session >`.
1159
+
1158
1160
When using :ref: `autowiring <services-autowire >`, type-hint any controller or
1159
1161
service argument with the :class: `Symfony\\ Component\\ Translation\\ LocaleSwitcher `
1160
1162
class to inject the locale switcher service. Otherwise, configure your services
0 commit comments