Skip to content

Commit db92b3a

Browse files
[Translation] Give current locale to LocaleSwitcher::runWithLocale()'s callback
1 parent 044ee18 commit db92b3a

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

translation.rst

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1062,17 +1062,32 @@ of:
10621062
$this->localeSwitcher->reset();
10631063

10641064
// you can also run some code with a certain locale, without
1065-
// changing the locale for the rest of the application
1065+
// changing the locale for the rest of the application.
10661066
$this->localeSwitcher->runWithLocale('es', function() {
10671067

10681068
// e.g. render here some Twig templates using 'es' (Spanish) locale
10691069

10701070
});
10711071

1072+
// you can optionally declare an argument in your callback to receive the
1073+
// injected locale
1074+
$this->localeSwitcher->runWithLocale('es', function(string $locale) {
1075+
1076+
// here, the $locale argument will be set to 'es'
1077+
1078+
});
1079+
10721080
// ...
10731081
}
10741082
}
10751083

1084+
.. versionadded:: 6.4
1085+
1086+
The support of declaring an argument in the callback to inject the locale
1087+
being used in the
1088+
:method:`Symfony\\Component\\Translation\\LocaleSwitcher::runWithLocale`
1089+
method was introduced in Symfony 6.4.
1090+
10761091
When using :ref:`autowiring <services-autowire>`, type-hint any controller or
10771092
service argument with the :class:`Symfony\\Component\\Translation\\LocaleSwitcher`
10781093
class to inject the locale switcher service. Otherwise, configure your services

0 commit comments

Comments
 (0)