How to specify browser language in Puppeteer
There are several ways to change locale, you can try all of them to find what works for you,
Use Args when launching
const browser = await puppeteer.launch({
headless: false,
args: ['--lang=bn-BD,bn']
});
Send the language as Header
await page.setExtraHTTPHeaders({
'Accept-Language': 'bn'
});
Forcefully set the language
// Set the language forcefully on javascript
await page.evaluateOnNewDocument(() => {
Object.defineProperty(navigator, "language", {
get: function() {
return "bn-BD";
}
});
Object.defineProperty(navigator, "languages", {
get: function() {
return ["bn-BD", "bn"];
}
});
});
For the sake of testing, I'll test this in multiple languages, including es
, and here is the result.
Google search:
BrowserLeaks:
There's an error in Md-Abu-Taher's answer.
The response to navigator.language
should return a string, not an array. Try it in your own browser console.
The code snippet should be:
await page.evaluateOnNewDocument(() => {
Object.defineProperty(navigator, "language", {
get: function() {
return "en-GB";
}
});
Object.defineProperty(navigator, "languages", {
get: function() {
return ["en-GB", "en"];
}
});
});