Skip to content

Instantly share code, notes, and snippets.

@ksol

ksol/chrome_39.js

Last active Oct 10, 2020
Embed
What would you like to do?
Language detection in javascript
window.navigator.language // -> "fr"
window.navigator.languages // -> ["fr-FR", "fr", "en-US", "en", "es", "de"]
window.navigator.userLanguage // -> undefined
window.navigator.browserLanguage // -> undefined
window.navigator.systemLanguage // -> undefined
window.navigator.language // -> "fr"
window.navigator.languages // -> [ "fr", "fr-FR", "en-US", "en" ]
window.navigator.userLanguage // -> undefined
window.navigator.browserLanguage // -> undefined
window.navigator.systemLanguage // -> undefined
window.navigator.language // -> undefined
window.navigator.languages // -> undefined
window.navigator.userLanguage // -> 'en-us'
window.navigator.browserLanguage // -> 'en-us'
window.navigator.systemLanguage // -> 'en-us'
window.navigator.language // -> undefined
window.navigator.languages // -> undefined
window.navigator.userLanguage // -> 'en-US'
window.navigator.browserLanguage // -> 'en-US'
window.navigator.systemLanguage // -> 'en-US'
window.navigator.language // -> 'en-US'
window.navigator.languages // -> undefined
window.navigator.userLanguage // -> 'en-US'
window.navigator.browserLanguage // -> 'en-US'
window.navigator.systemLanguage // -> 'en-US'
window.navigator.language // -> "fr-fr"
window.navigator.languages // -> undefined
window.navigator.userLanguage // -> undefined
window.navigator.browserLanguage // -> undefined
window.navigator.systemLanguage // -> undefined
@Slumber86

This comment has been minimized.

Copy link

@Slumber86 Slumber86 commented Apr 27, 2017

Safari 10:

window.navigator.language        // -> "fr-FR"
window.navigator.languages       // -> ["fr-FR", "fr", "en-US", "en", "es", "de"]
window.navigator.userLanguage    // -> undefined
window.navigator.browserLanguage // -> undefined
window.navigator.systemLanguage  // -> undefined 
@DarkMatterMatt

This comment has been minimized.

Copy link

@DarkMatterMatt DarkMatterMatt commented Mar 18, 2018

chrome_65 is still

window.navigator.language        // -> "fr"
window.navigator.languages       // -> ["fr-FR", "fr", "en-US", "en", "es", "de"]
window.navigator.userLanguage    // -> undefined
window.navigator.browserLanguage // -> undefined
window.navigator.systemLanguage  // -> undefined
@alailsonribeiro

This comment has been minimized.

Copy link

@alailsonribeiro alailsonribeiro commented Apr 2, 2020

Direct in JS

let lg = {
  a: navigator.language,
  b: navigator.languages,
  c: navigator.userLanguage,
  d: navigator.browserLanguage,
  e: navigator.systemLanguage
},   language =  {
    detected: lg.a || lg.c || lg.d || lg.e || lg.b[0],
    available: lg.b || [lg.a] || [lg.c] || [lg.d] || [lg.e]
  }

Direct in Browser

let lg = {
  a: window.navigator.language,
  b: window.navigator.languages,
  c: window.navigator.userLanguage,
  d: window.navigator.browserLanguage,
  e: window.navigator.systemLanguage
},   language =  {
    detected: lg.a || lg.c || lg.d || lg.e || lg.b[0],
    available: lg.b || [lg.a] || [lg.c] || [lg.d] || [lg.e]
  }

Sample of return in Chrome
{ detected: "pt-BR", available: ["pt-BR", "pt", "en-US", "en"] }

Sample of return in Safari 10
{ detected: "pt-BR", available: ["pt-BR"] }

+info: https://github.com/alailsonribeiro/locales/tree/master/language

@basvandorst

This comment has been minimized.

Copy link

@basvandorst basvandorst commented Oct 8, 2020

error TS2339: Property 'systemLanguage' does not exist on type 'Navigator'

To avoid errors this Typescript error you have to change the last 3 lines of code:

 let language = {
      a: window.navigator.language,
      b: window.navigator.languages,
      c: window.navigator['userLanguage'],
      d: window.navigator['browserLanguage'],
      e: window.navigator['systemLanguage'],
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.