Contributed by Kevin Bond in #45793.
Working with the locale in Symfony applications is simple. The locale is stored in a request attribute called _locale and used in the rest of the application. Changing the locale (e.g. in multilingual applications via a list of languages selectable by users) requires changing that request attribute in an early event listener. However, sometimes you need to change the locale of the application dynamically just to run some code. Imagine an application that renders Twig templates of emails in different languages. You need to change the locale only to render those templates. In Symfony 6.1 we're improving situations like this thanks to a new locale switcher. The LocaleSwitcher object/service allows you to change at once the locale of all the services that are tagged with kernel.locale_aware.
use Symfony\Component\Translation\LocaleSwitcher;
class SomeClass { public function __construct( private LocaleSwitcher $localeSwitcher, ) {}
public function someMethod()
{
// you can get the current application locale like this:
$currentLocale = $this->localeSwitcher->getLocale();
// you can set the locale for the entire application like this:
// (from now on, the application will use 'fr' (French) as the
// locale; including the default locale used to translate Twig templates)
$this->localeSwitcher->setLocale('fr');
// you can also run some code with a certain locale, without
// changing the locale for the rest of the application
$this->localeSwitcher->runWithLocale('es', function() {
// e.g. render here some Twig templates using 'es' (Spanish) locale
});
// ...
}
}
Sponsor the Symfony project.
Jelentkezéshez jelentkezzen be
EGYÉB POSTS Ebben a csoportban

Symfony's bridge packages integrate third-party services, such as mailers, notifiers, and translation providers, into Symfony applications. With more than 120 bridges available today, Symfony supports

Symfony Messenger component keeps evolving to meet the needs of complex, modern applications. In Symfony 7.3, we're introducing several powerful features to it.
Run Process Using the Shell… https://s

The Routing component provides an impressive list of features to map incoming URLs to your application code. Symfony 7.3 pushes it even further with a set of new features that improve developer experi

Contributed by Mathias Arlaud in

Affected versions
Symfony UX symfony/ux-live-component and symfony/ux-twig-component versions <2.25.1 are affected by this security issue.
The issue has been fixed in the 2.25.1 version of these

Symfony has been reducing the need for configuration in applications for several years now. Thanks to PHP attributes, you can now configure most things alongside the relevant code, removing the need f

This week, development activity focused on polishing Symfony 7.3 ahead of its final release in two weeks. We also continued publishing articles highlighting the new features of Symfony 7.3 and shared