diff --git a/src/Api/PendingAwaitablePage.php b/src/Api/PendingAwaitablePage.php index 83ad7b77..140aee51 100644 --- a/src/Api/PendingAwaitablePage.php +++ b/src/Api/PendingAwaitablePage.php @@ -173,7 +173,7 @@ private function buildAwaitablePage(array $options): AwaitableWebpage $browser = Playwright::browser($this->browserType)->launch(); $context = $browser->newContext([ - 'locale' => 'en-US', + 'locale' => Playwright::defaultLocale(), 'timezoneId' => 'UTC', 'colorScheme' => Playwright::defaultColorScheme()->value, ...$this->device->context(), diff --git a/src/Configuration.php b/src/Configuration.php index bf3c458f..11ccc2f3 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -105,6 +105,16 @@ public function withHost(?string $host): self return $this; } + /** + * Sets the default locale for the browser context. + */ + public function withLocale(string $locale): self + { + Playwright::setDefaultLocale($locale); + + return $this; + } + /** * Enables debug mode for assertions. */ diff --git a/src/Playwright/Playwright.php b/src/Playwright/Playwright.php index c0cffae1..2c5f6d9d 100644 --- a/src/Playwright/Playwright.php +++ b/src/Playwright/Playwright.php @@ -59,6 +59,11 @@ final class Playwright */ private static ?string $host = null; + /** + * The default locale. + */ + private static string $defaultLocale = 'en-US'; + /** * Get a browser factory for the given browser type. */ @@ -155,6 +160,22 @@ public static function host(): ?string return self::$host; } + /** + * Set the default locale. + */ + public static function setDefaultLocale(string $locale): void + { + self::$defaultLocale = $locale; + } + + /** + * Get the default locale. + */ + public static function defaultLocale(): string + { + return self::$defaultLocale; + } + /** * Get the default color scheme. */ diff --git a/tests/Unit/Configuration/LocaleConfigurationTest.php b/tests/Unit/Configuration/LocaleConfigurationTest.php new file mode 100644 index 00000000..ebc19f44 --- /dev/null +++ b/tests/Unit/Configuration/LocaleConfigurationTest.php @@ -0,0 +1,50 @@ +withLocale('fr-FR'); + + expect($result)->toBeInstanceOf(Configuration::class); + expect(Playwright::defaultLocale())->toBe('fr-FR'); +}); + +it('follows fluent interface pattern', function (): void { + $config = new Configuration(); + + $result = $config + ->withLocale('fr-FR') + ->userAgent('Test Agent') + ->timeout(10000); + + expect($result)->toBeInstanceOf(Configuration::class); + expect(Playwright::defaultLocale())->toBe('fr-FR'); +}); + +it('stores locale in Playwright global state', function (): void { + expect(Playwright::defaultLocale())->toBe('en-US'); + + Playwright::setDefaultLocale('ja-JP'); + + expect(Playwright::defaultLocale())->toBe('ja-JP'); +}); + +it('can override locale multiple times', function (): void { + Playwright::setDefaultLocale('fr-FR'); + expect(Playwright::defaultLocale())->toBe('fr-FR'); + + Playwright::setDefaultLocale('de-DE'); + expect(Playwright::defaultLocale())->toBe('de-DE'); + + Playwright::setDefaultLocale('ja-JP'); + expect(Playwright::defaultLocale())->toBe('ja-JP'); +});