How to specify browser language in Puppeteer
Asked Answered
J

6

28

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

Juggle answered 24/10, 2017 at 10:53 Comment(2)
Is passing language arguments works when starting Chromium browser from the terminal?Huckster
I have the same problem, "window.navigator" is undefined in Puppeteer's headless Chromium, even when using the --lang switch.Halves
N
70

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 image description here

Nieman answered 14/11, 2017 at 17:38 Comment(5)
Mind you, that args solution doesnt work on mac osMechanician
Is there another way for mac OS X?Martinic
At Chromium issue 755338 they write that setting environment variable LANG should work as well on Linux. Nothing like that for Windows though, it seems...Carrycarryall
Neither of them worked for meIncapacious
The "Forcefully set the language" bit doesn't override languages for me in Puppeteer 22.7.1, Google Chrome for Testing 124.0.6367.78 (Official Build) (64-bit), on Ubuntu 22.04.Diploblastic
D
8

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"];
        }
    });
});
Dyanne answered 29/11, 2019 at 11:8 Comment(1)
Thank you for pointing that out, I've updated my answer.Nieman
F
2

On Chrome Linux, you can do

puppeteer.launch({ ...yourParams, env: { LANGUAGE: "fr_FR" } })

I don't know for the others OS/browsers ...

Feature answered 24/2, 2022 at 16:23 Comment(1)
funny enough my puppeteer fails to start on linux when i tried adding this env variable... super weird "Error: Failed to launch the browser process! undefined"Submission
C
1

Additional info to the accepted answer:

If you want to dynamically change the locale via the navigator.language property in puppeteer you need to pass your variable as an argument array as a second parameter of the evaluateOnNewDocument() function like this:

const locale = req.body.localization;

// add locale
await page.evaluateOnNewDocument((args) => {
    Object.defineProperty(navigator, "language", {
        get: function () {
            return args[0];
        }
    });
}, [locale]);

If you want to directly use the locale variable inside the evaluateOnNewDocument() function, it will be undefined and puppeteer will possibly render an empty page. Therefore you will need this workaround with the argument

Correlate answered 10/4, 2024 at 7:33 Comment(0)
E
1

I've been using --lang=.. that worked nearly everywhere: locally on my windows and in a docker instance on WSL ubuntu. On a hyper-v teamcity agent, where I started executing unit tests, I noticed that for whatever reason, setting the language failed.

I fixed in once, setting the ENV var LANG=xx to something but reckoned this was annoying and also did sometimes not work.

The fix was based on the list of command switches for chromium

Solution

Also add --accept-lang=XX to puppeteer startup args. This now seems to work. For german the startup code looks like:

 await puppeteer.launch(
            { 
                args: [
                    '--accept-lang=de',
                    '--lang=de'
                ]
            });
Englishism answered 29/6, 2024 at 17:44 Comment(0)
B
0

One more option using Chrome DevTools Protocol and Network.setUserAgentOverride:

const cdpSession = await page.createCDPSession();
cdpSession.send('Network.setUserAgentOverride', {
  userAgent: await browser.userAgent(),
  acceptLanguage: locale,
});
Borrowing answered 15/5, 2024 at 6:43 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.