Skip to content

Commit 49ee3a7

Browse files
committed
fix(ui): harden bootstrap locale fallback
1 parent 4bff81a commit 49ee3a7

3 files changed

Lines changed: 32 additions & 8 deletions

File tree

packages/ui/src/lib/i18n/index.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,15 @@ async function loadLocaleMessages(locale: Locale): Promise<Messages> {
117117

118118
export async function preloadLocaleMessages(preferredLocale?: string | null): Promise<Locale> {
119119
const resolvedLocale = matchSupportedLocale(preferredLocale ?? undefined) ?? detectNavigatorLocale() ?? "en"
120-
globalMessages = await loadLocaleMessages(resolvedLocale)
121-
setGlobalRevision((value) => value + 1)
122-
return resolvedLocale
120+
try {
121+
globalMessages = await loadLocaleMessages(resolvedLocale)
122+
setGlobalRevision((value) => value + 1)
123+
return resolvedLocale
124+
} catch {
125+
globalMessages = enMessages
126+
setGlobalRevision((value) => value + 1)
127+
return "en"
128+
}
123129
}
124130

125131
export function tGlobal(key: string, params?: TranslateParams): string {

packages/ui/src/lib/ui-bootstrap-cache.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export type UiBootstrapTheme = "light" | "dark" | "system"
22

33
export interface UiBootstrapCacheSnapshot {
4-
theme?: UiBootstrapTheme
5-
locale?: string
4+
theme?: UiBootstrapTheme | null
5+
locale?: string | null
66
}
77

88
const UI_BOOTSTRAP_CACHE_KEY = "codenomad:ui-bootstrap"
@@ -41,7 +41,18 @@ export function writeUiBootstrapCache(snapshot: UiBootstrapCacheSnapshot) {
4141
}
4242

4343
try {
44-
window.localStorage.setItem(UI_BOOTSTRAP_CACHE_KEY, JSON.stringify(snapshot))
44+
const previous = readUiBootstrapCache()
45+
const next: UiBootstrapCacheSnapshot = {
46+
theme: snapshot.theme === undefined ? previous.theme : snapshot.theme ?? undefined,
47+
locale: snapshot.locale === undefined ? previous.locale : snapshot.locale ?? undefined,
48+
}
49+
50+
if (!next.theme && !next.locale) {
51+
window.localStorage.removeItem(UI_BOOTSTRAP_CACHE_KEY)
52+
return
53+
}
54+
55+
window.localStorage.setItem(UI_BOOTSTRAP_CACHE_KEY, JSON.stringify(next))
4556
} catch {
4657
/* noop */
4758
}

packages/ui/src/stores/preferences.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,10 +600,17 @@ const configContextValue: ConfigContextValue = {
600600

601601
export const ConfigProvider: ParentComponent = (props) => {
602602
createEffect(() => {
603+
if (!isLoaded()) {
604+
return
605+
}
606+
603607
const bucket = uiConfigBucket()
608+
const theme = bucket.theme
609+
const locale = bucket.settings?.locale
610+
604611
writeUiBootstrapCache({
605-
theme: bucket.theme,
606-
locale: bucket.settings?.locale,
612+
theme: theme ?? null,
613+
locale: locale ?? null,
607614
})
608615
})
609616

0 commit comments

Comments
 (0)