Add check if navigator.languages exists to use

This commit is contained in:
gconsidine
2017-09-28 14:46:56 -04:00
parent 297904462d
commit d9e360e575

View File

@@ -28,7 +28,7 @@ function bootstrap (callback) {
* @returns {object=} - Locale data if it exists. * @returns {object=} - Locale data if it exists.
*/ */
function fetchLocaleStrings (callback) { function fetchLocaleStrings (callback) {
const code = normalizeLocaleCode(navigator.language || navigator.userLanguage); const code = getNormalizedLocaleCode();
if (isDefaultLocale(code) || !isSupportedLocale(code)) { if (isDefaultLocale(code) || !isSupportedLocale(code)) {
callback({ code }); callback({ code });
@@ -49,14 +49,29 @@ function fetchLocaleStrings (callback) {
request.fail(() => callback({ code: DEFAULT_LOCALE })); request.fail(() => callback({ code: DEFAULT_LOCALE }));
} }
function normalizeLocaleCode (code) { /**
* Grabs the language off of navigator for browser compatibility.
* If the language isn't set, then it falls back to the DEFAULT_LOCALE. The
* locale code is normalized to be lowercase and 2 characters in length.
*/
function getNormalizedLocaleCode () {
let code;
if (navigator.languages && navigator.languages[0]) {
[code] = navigator.languages;
} else if (navigator.language) {
code = navigator.language;
} else {
code = navigator.userLanguage;
}
try { try {
code = code.split('-')[0].toLowerCase(); code = code.split('-')[0].toLowerCase();
} catch (error) { } catch (error) {
code = DEFAULT_LOCALE; code = DEFAULT_LOCALE;
} }
return code; return code.substring(0, 2);
} }
function isSupportedLocale (code) { function isSupportedLocale (code) {