Use Navigator.language
or the first value of Navigator.languages
if available, otherwise return defaultLang
. Omit the second argument, defaultLang
, to use 'en-US'
as the default language code.
const detectLanguage = (defaultLang = 'en-US') =>
navigator.language ||
(Array.isArray(navigator.languages) && navigator.languages[0]) ||
defaultLang;
// EXAMPLE
detectLanguage(); // 'nl-NL'