@@ -1105,25 +1105,11 @@ Switch Locale Programmatically
1105
1105
1106
1106
The ``LocaleSwitcher `` was introduced in Symfony 6.1.
1107
1107
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.
1112
1111
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::
1127
1113
1128
1114
use Symfony\Component\Translation\LocaleSwitcher;
1129
1115
@@ -1136,28 +1122,23 @@ of:
1136
1122
1137
1123
public function someMethod(): void
1138
1124
{
1139
- // you can get the current application locale like this:
1140
1125
$currentLocale = $this->localeSwitcher->getLocale();
1141
1126
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.
1145
1129
$this->localeSwitcher->setLocale('fr');
1146
1130
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
1149
1133
$this->localeSwitcher->reset();
1150
1134
1151
- // you can also run some code with a certain locale, without
1135
+ // run some code with a specific locale, temporarily , without
1152
1136
// changing the locale for the rest of the application
1153
1137
$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
1157
1139
});
1158
1140
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:
1161
1142
$this->localeSwitcher->runWithLocale('es', function(string $locale) {
1162
1143
1163
1144
// here, the $locale argument will be set to 'es'
@@ -1175,6 +1156,20 @@ of:
1175
1156
:method: `Symfony\\ Component\\ Translation\\ LocaleSwitcher::runWithLocale `
1176
1157
method was introduced in Symfony 6.4.
1177
1158
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
+
1178
1173
When using :ref: `autowiring <services-autowire >`, type-hint any controller or
1179
1174
service argument with the :class: `Symfony\\ Component\\ Translation\\ LocaleSwitcher `
1180
1175
class to inject the locale switcher service. Otherwise, configure your services
0 commit comments