Monday, May 20, 2024
78
rated 0 times [  79] [ 1]  / answers: 1 / hits: 26595  / 7 Years ago, tue, october 24, 2017, 12:00:00

I would like to launch a Google Chrome browser with language Spanish es using Puppeteer.



I've tried puppeteer.launch(args:['--lang=es',...],...) but it didn't work.



I've tried passing the environment variable LANGUAGE=es mocha puppeteer-test.js but it didn't work.



I've tried using the userDataDir option and passing a folder with a Preferences file a { intl: { accept_languages: es } } but the browser Settings - Languages still don't show Spanish and neither does window.navigator.languages neither window.navigator.language



I'm using

Puppeteer 0.11.0

Node 8.4.0

NPM 5.2.0

macOS El Capitan 10.11.6

MacBook Pro Retina, 15-inch, Mid 2015


More From » google-chrome

 Answers
11

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:


es
bn


BrowserLeaks:


enter


[#56135] Saturday, October 21, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
chase

Total Points: 78
Total Questions: 106
Total Answers: 93

Location: Comoros
Member since Tue, Mar 14, 2023
1 Year ago
chase questions
Thu, Mar 31, 22, 00:00, 2 Years ago
Thu, Jul 1, 21, 00:00, 3 Years ago
Sat, Dec 12, 20, 00:00, 4 Years ago
Mon, Sep 14, 20, 00:00, 4 Years ago
;