ammar
ammar

Reputation: 1393

How do I change the browser language with Javascript

I want to access the user browser settings and change the browser language, is this possible using Javascript?

If it's possible, how can I access this? (sample code please)

Upvotes: 19

Views: 45485

Answers (6)

Igor Bykov
Igor Bykov

Reputation: 2812

This is possible if you do it from within a Chrome extension.

Check this answer to a similar question & contentScript.js of locale-switcher Chrome extension:

let locale = null;

chrome.storage.local.get(["locale"], result => {
  if (result) {
    locale = result.locale;
  }

  if (locale) embedScript();
});

const embedScript = () => {
  const code = `
    (() => {
      Object.defineProperties(Navigator.prototype, {
        language: {
          value: '${locale}',
          configurable: false,
          enumerable: true,
          writable: false
        },
        languages: {
          value: ['${locale}'],
          configurable: false,
          enumerable: true,
          writable: false
        }
      });
    })();`;

  const script = document.createElement("script");
  script.textContent = code;
  document.documentElement.prepend(script);
  script.remove();
};

I'm aware of timing (this question was asked 10 years before this answer) & that would be particularly fun if you are the author of this extension.

Upvotes: 5

Randy the Dev
Randy the Dev

Reputation: 26700

If what you actually want to do is detect the language the user is using, which is what you want to do, because nothing will annoy your visitors more that their browser preferences getting changed, on the server-side, read the Accept-Language HTTP request header that all modern browsers send, it should contain all the info you need. If it is absent, assume the language of your largest audience.

Check out RFC2616 Section 14.4 for more information on Accept-Language and it's use.

Upvotes: 2

vol7ron
vol7ron

Reputation: 42099

You can detect, but cannot set.

var lang = navigator.language || navigator.userLanguage;

// navigator.language     : Netscape & Firefox
// navigator.userLanguage : Internet Explorer


If you want to output different languages, the best way is to do it server-side. Either:

  • use an AJAX call to dynamically load the appropriate page
  • use a session variable to load the initial page correctly

Upvotes: 12

Eric Mickelsen
Eric Mickelsen

Reputation: 10377

This is impossible and a bad idea. A better idea is to detect the browser's language, which is possible to do reasonably well, and ask the user to change it (assuming the change is absolutely necessary).

Upvotes: 6

Lekensteyn
Lekensteyn

Reputation: 66395

No that is not possible. How would you find it if you open a page, and your browser turns Arabic (or some other language you can't read)?

Upvotes: 5

Pekka
Pekka

Reputation: 449385

This is definitely not possible using JavaScript on a web page.

A browser Extension might have the rights to change this - I'm not sure, it will also depend on the browser. However, building such an extension would require a fair amount of skill and work.

Upvotes: 1

Related Questions