Skip to content

Add withLocale() to global browser configuration #1659

@ThibaudDauce

Description

@ThibaudDauce

Problem

withLocale() exists on PendingAwaitablePage (per-visit level):

$page = visit('/')->withLocale('fr-FR');

But there is no equivalent on the global pest()->browser() configuration (Configuration class). The default locale is hardcoded to en-US in PendingAwaitablePage::buildAwaitablePage():

$context = $browser->newContext([
    'locale' => 'en-US',
    // ...
    ...$options,
]);

This means every visit() call defaults to en-US for navigator.language, and there's no way to change this globally without passing ['locale' => 'fr'] to every single visit() call.

Use case

Applications that detect the browser language via navigator.language to set the UI language need the Playwright browser context to use the correct locale. With 60+ test files and 179 visit() calls, passing the locale to each one is not practical.

Desired behavior

// tests/Pest.php
pest()->browser()->withLocale('fr');

Similar to how withHost(), userAgent(), timeout(), inDarkMode() etc. already work on Configuration.

Suggested implementation

  1. Add Playwright::setDefaultLocale(string $locale) and Playwright::defaultLocale() (similar to existing setHost/host pattern)
  2. Add Configuration::withLocale(string $locale) that calls Playwright::setDefaultLocale()
  3. Use Playwright::defaultLocale() ?? 'en-US' in PendingAwaitablePage::buildAwaitablePage()

Environment

  • pest-plugin-browser: 4.0
  • Pest: 4.x
  • PHP: 8.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions