New in Symfony 6.1: Locale Switcher

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.

https://symfony.com/blog/new-in-symfony-6-1-locale-switcher?utm_source=Symfony%20Blog%20Feed&utm_medium=feed

Létrehozva 3y | 2022. ápr. 14. 9:20:06


Jelentkezéshez jelentkezzen be

EGYÉB POSTS Ebben a csoportban

New in Symfony 7.3: New Bridges and Improved Integrations

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

2025. máj. 23. 9:30:04 | Symfony
New in Symfony 7.3: Messenger Improvements

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

2025. máj. 22. 7:50:11 | Symfony
New in Symfony 7.3: Routing Improvements

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

2025. máj. 21. 11:10:07 | Symfony
New in Symfony 7.3: JsonStreamer Component

Contributed by Mathias Arlaud in

2025. máj. 20. 9:30:13 | Symfony
Symfony UX CVE-2025-47946: Unsanitized HTML attribute injection via ComponentAttributes

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

2025. máj. 19. 12:40:14 | Symfony
New in Symfony 7.3: Yaml Improvements

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

2025. máj. 19. 8:10:09 | Symfony
A Week of Symfony #959 (May 12–18, 2025)

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

2025. máj. 18. 8:50:08 | Symfony