Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d9f050a

Browse filesBrowse files
committed
Merge branch '6.4' into 7.2
* 6.4: Rewords Clarifying LocaleSwitcher's behavior
2 parents 24aecee + aa698f8 commit d9f050a
Copy full SHA for d9f050a

File tree

1 file changed

+25
-23
lines changed
Filter options

1 file changed

+25
-23
lines changed

‎translation.rst

Copy file name to clipboardExpand all lines: translation.rst
+25-23Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,18 +1099,11 @@ checks translation resources for several locales:
10991099
Switch Locale Programmatically
11001100
------------------------------
11011101

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.
11061105

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::
11141107

11151108
use Symfony\Component\Translation\LocaleSwitcher;
11161109

@@ -1123,28 +1116,23 @@ of:
11231116

11241117
public function someMethod(): void
11251118
{
1126-
// you can get the current application locale like this:
11271119
$currentLocale = $this->localeSwitcher->getLocale();
11281120

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.
11321123
$this->localeSwitcher->setLocale('fr');
11331124

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
11361127
$this->localeSwitcher->reset();
11371128

1138-
// you can also run some code with a certain locale, without
1129+
// run some code with a specific locale, temporarily, without
11391130
// changing the locale for the rest of the application
11401131
$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
11441133
});
11451134

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:
11481136
$this->localeSwitcher->runWithLocale('es', function(string $locale) {
11491137

11501138
// here, the $locale argument will be set to 'es'
@@ -1155,6 +1143,20 @@ of:
11551143
}
11561144
}
11571145

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+
11581160
When using :ref:`autowiring <services-autowire>`, type-hint any controller or
11591161
service argument with the :class:`Symfony\\Component\\Translation\\LocaleSwitcher`
11601162
class to inject the locale switcher service. Otherwise, configure your services

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.