diff --git a/lib/common.js b/lib/common.js index 5bdcb73..978cde9 100644 --- a/lib/common.js +++ b/lib/common.js @@ -93,7 +93,7 @@ class DocumentLocaleSettings { constructor() { this._cache = new Map(); - this._htmlElem = window.document.getElementsByTagName('html')[0]; + this._htmlElem = document.documentElement; this._listeners = []; this._overrides = {}; this._observer = new MutationObserver(this._handleObserverChange.bind(this)); @@ -227,8 +227,16 @@ export function getDocumentLocaleSettings() { return documentLocaleSettings; } +const localeRegEx = /[^a-zA-Z0-9-]/g; + function updateLocalNames() { - const localName = new Intl.DisplayNames(documentLocaleSettings.language || navigator.language, { type: 'language' }); + const possibleLocales = [documentLocaleSettings.language, navigator.language, defaultLocale].filter(l => l && !localeRegEx.test(l)); + let localName; + try { + localName = new Intl.DisplayNames(possibleLocales, { type: 'language' }); + } catch { + return; + } supportedLocalesDetails.forEach(l => { l.localName = localName.of(l.overrideCode || l.code); });