Skip to content

Commit

Permalink
Merge pull request #239 from BrightspaceUI/dgleckler/filter-locales
Browse files Browse the repository at this point in the history
Filter out bad locales and fail gracefully
  • Loading branch information
bearfriend authored Mar 10, 2025
2 parents e698aef + f556646 commit 00bc79d
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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);
});
Expand Down

0 comments on commit 00bc79d

Please sign in to comment.