From d4b406de048590056d1afeef4ec83af291fb597e Mon Sep 17 00:00:00 2001 From: Danny Gleckler Date: Mon, 10 Mar 2025 16:26:17 -0400 Subject: [PATCH 1/2] Filter out bad locales --- lib/common.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/common.js b/lib/common.js index 5bdcb73..a8e492e 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,11 @@ 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)); + const localName = new Intl.DisplayNames(possibleLocales, { type: 'language' }); supportedLocalesDetails.forEach(l => { l.localName = localName.of(l.overrideCode || l.code); }); From f5566463a2fa13705f764ef04044196b218d2743 Mon Sep 17 00:00:00 2001 From: Danny Gleckler Date: Mon, 10 Mar 2025 16:37:56 -0400 Subject: [PATCH 2/2] Fail gracefully --- lib/common.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/common.js b/lib/common.js index a8e492e..978cde9 100644 --- a/lib/common.js +++ b/lib/common.js @@ -231,7 +231,12 @@ const localeRegEx = /[^a-zA-Z0-9-]/g; function updateLocalNames() { const possibleLocales = [documentLocaleSettings.language, navigator.language, defaultLocale].filter(l => l && !localeRegEx.test(l)); - const localName = new Intl.DisplayNames(possibleLocales, { type: 'language' }); + let localName; + try { + localName = new Intl.DisplayNames(possibleLocales, { type: 'language' }); + } catch { + return; + } supportedLocalesDetails.forEach(l => { l.localName = localName.of(l.overrideCode || l.code); });