Playwright Locale & Language for Screenshots
In this article
Setting the browser locale and language in Playwright controls how the browser formats dates, numbers, and strings, and which language UI text appears in. This is essential for screenshot testing of localized websites, ensuring your captures match the intended regional audience. You can achieve this by passing locale and language options when launching a browser context.
What You'll Need
- Node.js installed (v16 or later recommended).
- A Playwright project set up with the library installed (
npm init playwright@latest). - Basic familiarity with JavaScript/TypeScript and Playwright's test runner or scripting API.
Step-by-Step
- Identify the target locale and language. Locale codes follow the BCP 47 format, like
en-US,fr-FR,de-DE, orja-JP. The language (for thelangattribute) is usually the same code, but you can set them independently if needed (e.g., localeen-GBwith languageen). - Create a browser context with the locale. In your test or script, use
browser.newContext({ locale: 'fr-FR' }). This sets the locale for the entire context, affecting date/time formatting, number separators, and default language forAccept-Languageheader. - Set the language (optional). If you need the
langattribute on theelement to be different from the locale (rare), you can override it usingpage.addInitScriptto setdocument.documentElement.lang = 'fr'. However, most sites read the locale automatically. - Launch the page and take a screenshot. Navigate to your target URL, wait for the page to load, and use
page.screenshot({ path: 'screenshot.png' }). The screenshot will reflect the locale settings. - Use the test runner for convenience. In Playwright Test, you can define a custom project in
playwright.config.tswithuse: { locale: 'de-DE' }. This applies the locale to all tests in that project, making it easy to run multi-locale screenshot suites. - Combine with other context options. You can set locale along with other options like
timezoneId(e.g., 'Europe/Berlin') andgeolocationto simulate a real user in a specific region. This improves realism for screenshots. - Verify the result. Open the screenshot and check that the text is in the expected language and date formats appear correctly (e.g., 24-hour clock vs 12-hour). If not, proceed to troubleshooting.
Common Mistakes
- Setting locale on the browser instead of the context. Playwright's
browser.newContext()is where locale belongs;browser.newPage()does not accept locale directly. - Using invalid locale codes. For example,
enis a language code, not a full locale. Useen-USoren-GBfor proper formatting. - Forgetting to wait for the page to load. If you take a screenshot immediately after navigation, the page may not have applied the locale-specific content yet. Use
page.waitForLoadState('networkidle')or a selector wait. - Assuming the site respects the locale. Some websites ignore the browser locale and use IP-based geolocation or a user-selected language. You may need to also set cookies or localStorage.
Tips
- Use a consistent locale for all screenshots. Define a list of locales and loop through them to generate a full set of localized screenshots.
- Set the
langattribute manually if needed. Some automated accessibility checks rely on it. Usepage.addInitScript(() => document.documentElement.lang = 'fr')after setting locale. - Combine with device emulation. Use
devices['iPhone 13']to get mobile viewport and locale together for mobile screenshots. - Keep locale codes in a separate config file. This makes it easy to add new locales without editing test code.
- Test with a real user agent. Set
userAgentto match the locale (e.g., a French browser) to avoid detection as a bot.
Troubleshooting
Screenshot still shows English text. First, verify the locale is applied by checking navigator.language in the page context. If it returns the correct locale, the site may be ignoring it. Try setting a cookie like lang=fr if the site uses that. If the site uses IP geolocation, you may need to use a proxy (not recommended) or manually set localStorage via page.addInitScript.
Date formats are not changing. Some sites use JavaScript libraries that format dates based on the browser's Intl API. Ensure the locale is set before the page loads. If the site uses a hardcoded format, you may need to mock the Intl object, but that's complex. Alternatively, use a different locale that matches the desired format.
The lang attribute is empty or wrong. This is usually because the site sets it dynamically. Use page.addInitScript to override it after the page loads, but before screenshots. Or use page.evaluate to set it just before taking the screenshot.
Locale works in one browser but not another. Chromium and Firefox handle locale differently. Test in all target browsers. If using WebKit, ensure the locale is supported.
Frequently Asked Questions
Can I set locale per page instead of per context?
No, Playwright only supports locale at the context level. You can create a new context per page if you need different locales.
Does setting locale affect the Accept-Language header?
Yes, Playwright sets the Accept-Language header to the locale value by default, which helps servers return localized content.
How do I set both locale and timezone?
Use contextOptions with both locale and timezoneId. For example: newContext({ locale: 'ja-JP', timezoneId: 'Asia/Tokyo' }).
Will the locale affect the screenshot of a PDF or canvas?
Yes, if the PDF or canvas renders text, it will use the locale for formatting. However, canvas rendering may not respect locale for all text, depending on the library used.
Can I change the locale after the context is created?
No, locale is immutable after context creation. You need to create a new context with the new locale.
Is there a way to set the language without changing the locale?
Yes, use page.addInitScript to set document.documentElement.lang manually. This does not affect number/date formatting, only the lang attribute.