How to change locale in Symfony5 / PHPUnit before calling request?



PHP Snippet 1:

class MyTest extends WebTestCase
{
    public function testIndex()
    {
        $client = static::createClient();

        $session = self::$container->get(SessionInterface::class);
        $session->set('_locale', 'de');

        $client->request('GET', '/dashboard');
        // The resulting page is in German
        ....
    }
}

PHP Snippet 2:

class MyTest extends WebTestCase
{
    public function testIndex()
    {
        $client = static::createClient();

        $session = self::$container->get(SessionInterface::class);
        $session->set('_locale', 'de');

        $client->request('GET', '/dashboard');
        // The resulting page is in English (my default locale)
        ....

        $session->set('_locale', 'nl');

        $client->request('GET', '/dashboard');
        // The resulting page is in German
        ....
    }
}

PHP Snippet 3:

    $container = static::getContainer();
    /** @var SessionInterface $session */
    $session = $container->get('session');
    $session->set('_locale', 'de');
    $session->save();
    dump($session->get('_locale')); //de

    $session->set('_locale', 'en');
    $session->save();
    dump($session->get('_locale')); //en