TypeError: window.navigator.userLanguage is undefined

This can happen, when the user deletes the pref User languages from the
browser.

Change-Id: Ieaf783fbae113724704e2cce2e166e56935e8ce1
This commit is contained in:
umherirrender
2013-07-12 20:36:31 +02:00
committed by Umherirrender
parent 723d564fe4
commit 92a021e147

View File

@@ -75,8 +75,18 @@
return $.parseJSON( previousLanguages ).slice( -5 );
};
/**
* Returns the browser's user interface language or the system language.
* The caller should check the validity of the returned language code.
*
* @return {string} Language code or empty string.
*/
mw.uls.getBrowserLanguage = function () {
return ( window.navigator.language || window.navigator.userLanguage ).split( '-' )[0];
// language is the standard property.
// userLanguage is only for IE and returns system locale.
// Empty string is a fallback in case both are undefined
// to avoid runtime error with split().
return ( window.navigator.language || window.navigator.userLanguage || '' ).split( '-' )[0];
};
/*jshint camelcase:false*/