fix(ui): harden bootstrap locale fallback
This commit is contained in:
@@ -117,9 +117,15 @@ async function loadLocaleMessages(locale: Locale): Promise<Messages> {
|
||||
|
||||
export async function preloadLocaleMessages(preferredLocale?: string | null): Promise<Locale> {
|
||||
const resolvedLocale = matchSupportedLocale(preferredLocale ?? undefined) ?? detectNavigatorLocale() ?? "en"
|
||||
globalMessages = await loadLocaleMessages(resolvedLocale)
|
||||
setGlobalRevision((value) => value + 1)
|
||||
return resolvedLocale
|
||||
try {
|
||||
globalMessages = await loadLocaleMessages(resolvedLocale)
|
||||
setGlobalRevision((value) => value + 1)
|
||||
return resolvedLocale
|
||||
} catch {
|
||||
globalMessages = enMessages
|
||||
setGlobalRevision((value) => value + 1)
|
||||
return "en"
|
||||
}
|
||||
}
|
||||
|
||||
export function tGlobal(key: string, params?: TranslateParams): string {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
export type UiBootstrapTheme = "light" | "dark" | "system"
|
||||
|
||||
export interface UiBootstrapCacheSnapshot {
|
||||
theme?: UiBootstrapTheme
|
||||
locale?: string
|
||||
theme?: UiBootstrapTheme | null
|
||||
locale?: string | null
|
||||
}
|
||||
|
||||
const UI_BOOTSTRAP_CACHE_KEY = "codenomad:ui-bootstrap"
|
||||
@@ -41,7 +41,18 @@ export function writeUiBootstrapCache(snapshot: UiBootstrapCacheSnapshot) {
|
||||
}
|
||||
|
||||
try {
|
||||
window.localStorage.setItem(UI_BOOTSTRAP_CACHE_KEY, JSON.stringify(snapshot))
|
||||
const previous = readUiBootstrapCache()
|
||||
const next: UiBootstrapCacheSnapshot = {
|
||||
theme: snapshot.theme === undefined ? previous.theme : snapshot.theme ?? undefined,
|
||||
locale: snapshot.locale === undefined ? previous.locale : snapshot.locale ?? undefined,
|
||||
}
|
||||
|
||||
if (!next.theme && !next.locale) {
|
||||
window.localStorage.removeItem(UI_BOOTSTRAP_CACHE_KEY)
|
||||
return
|
||||
}
|
||||
|
||||
window.localStorage.setItem(UI_BOOTSTRAP_CACHE_KEY, JSON.stringify(next))
|
||||
} catch {
|
||||
/* noop */
|
||||
}
|
||||
|
||||
@@ -600,10 +600,17 @@ const configContextValue: ConfigContextValue = {
|
||||
|
||||
export const ConfigProvider: ParentComponent = (props) => {
|
||||
createEffect(() => {
|
||||
if (!isLoaded()) {
|
||||
return
|
||||
}
|
||||
|
||||
const bucket = uiConfigBucket()
|
||||
const theme = bucket.theme
|
||||
const locale = bucket.settings?.locale
|
||||
|
||||
writeUiBootstrapCache({
|
||||
theme: bucket.theme,
|
||||
locale: bucket.settings?.locale,
|
||||
theme: theme ?? null,
|
||||
locale: locale ?? null,
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user