How to Change Browser Language in Playwright
In this article
Changing the browser language in Playwright lets you test how your application behaves for users in different locales—date formats, currency, translations, and even text direction. You can set the language at the browser context level, which means each test can use a different language without affecting others. You'll need a working Playwright setup (Node.js or Python) and a basic test file.
What You'll Need
- A project with Playwright installed (
npm init playwright@latestorpip install playwright). - A test file where you create a browser context (or you can set it globally in
playwright.config). - Basic familiarity with Playwright's
browser.newContext()method.
Step-by-Step
- Open your test file where you define your test or script. For example,
test.spec.js(Node.js) ortest_playwright.py(Python). - Create a new browser context with the
localeoption. Thelocaleoption accepts a BCP 47 language tag like'en-US','fr-FR', or'de-DE'. In JavaScript:
In Python:const context = await browser.newContext({ locale: 'fr-FR' });context = browser.new_context(locale='fr-FR') - Create a page from that context. The page will inherit the locale. JavaScript:
const page = await context.newPage();Python:page = context.new_page(). - Navigate to your site and verify the language. For example, if your site detects the browser language, it should show French text when
locale: 'fr-FR'is set. - Set the locale globally (optional). If you want all tests to use a specific language, add the
localeproperty to theuseblock inplaywright.config.js(orplaywright.config.ts):use: { locale: 'es-ES' } - Run your test as usual. The browser will present the UI in the specified language.
Common Mistakes
- Setting the language on the browser instead of the context. Playwright's
browser.newContext()is where the locale belongs, notbrowser.newPage()or the browser launch options. - Using an invalid locale code. Playwright expects a valid BCP 47 tag.
'en'works, but'english'will throw an error. Check your tag against the IANA language subtag registry. - Forgetting that the site may override the browser language. Some sites store a language preference in cookies or localStorage. If you set the locale but the site still shows English, clear site data or use a fresh context.
- Using the locale option on an existing context. The locale is fixed at context creation. You cannot change it later; you must create a new context.
Tips
- Test multiple languages in one run by creating multiple contexts within the same test, each with a different locale.
- Combine locale with other context options like
timezoneIdandgeolocationto simulate a fully localized user. - Use the
acceptLanguageheader if your site relies on that. Playwright sets it automatically based on the locale, but you can override it viaextraHTTPHeaders. - Verify the language by checking an element's text with an assertion like
expect(page.locator('h1')).toHaveText('Bonjour'). - Keep a list of supported locales in your test config so you can quickly switch between them with an environment variable.
Troubleshooting
- The page still shows the default language. First, check that you passed the locale to
newContext()and not tonewPage(). Then, clear cookies/localStorage by using a fresh context or callingcontext.clearCookies(). Some sites also use server-side detection; in that case, set theAccept-Languageheader explicitly viaextraHTTPHeaders. - Playwright throws an error like “Invalid locale”. Double-check the spelling and format. Use lowercase for the language and uppercase for the region, e.g.,
'pt-BR'not'pt-br'(though Playwright is case-insensitive, it's good practice). - The locale works in one test but not another. If you're reusing a browser instance, each test should create its own context. If you set the locale in the config, ensure the config is actually being loaded—run with
npx playwright test --config=playwright.config.js. - Language changes after navigation. Some sites redirect to a language-specific URL. If you navigate to
example.comand it redirects toexample.com/fr, that's normal. To avoid redirects, navigate directly to the localized URL if you know it. - Headless vs headed mode differences. The locale should work the same in both, but if you're using a custom browser binary or a system locale override, ensure the OS has the necessary language packs installed.
Frequently Asked Questions
Can I change the language after the context is created?
No. The locale is fixed when you call browser.newContext(). To change it, create a new context.
Does setting the locale affect the Accept-Language header?
Yes. Playwright automatically sets the Accept-Language header based on the locale. For example, locale: 'fr-FR' sends Accept-Language: fr-FR.
What is the default locale in Playwright?
The default is 'en-US' unless you specify otherwise in the context options or the config file.
Can I set multiple languages for one context?
No, a context supports only one locale. To test multiple languages, create separate contexts or use parameterized tests.
Does the locale affect the browser's user agent?
No. The user agent remains the same. Only the language-related headers and internal browser settings change.
How do I verify the locale is actually applied?
You can navigate to a page that displays navigator.language or simply check the text of a localized element. In Playwright, you can also evaluate page.evaluate(() => navigator.language) to see the value.