Skip to content

Commit 06ea548

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

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

translation.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,27 @@ of:
10731073
}
10741074
}
10751075

1076+
.. tip::
1077+
1078+
If you need to get the locale used in the callback of the
1079+
``runWithLocale()`` method, you can declare the ``$locale`` special
1080+
argument like this::
1081+
1082+
public function someMethod(): void
1083+
{
1084+
// "es" will be injected in the `$locale` argument when the callback is executed
1085+
$this->localeSwitcher->runWithLocale('es', function(string $locale) {
1086+
// ...
1087+
});
1088+
1089+
// ...
1090+
}
1091+
1092+
.. versionadded:: 6.4
1093+
1094+
Declaring a ``$locale`` argument in the callback to inject the locale
1095+
being used was introduced in Symfony 6.4.
1096+
10761097
When using :ref:`autowiring <services-autowire>`, type-hint any controller or
10771098
service argument with the :class:`Symfony\\Component\\Translation\\LocaleSwitcher`
10781099
class to inject the locale switcher service. Otherwise, configure your services

0 commit comments

Comments
 (0)