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> {
|
export async function preloadLocaleMessages(preferredLocale?: string | null): Promise<Locale> {
|
||||||
const resolvedLocale = matchSupportedLocale(preferredLocale ?? undefined) ?? detectNavigatorLocale() ?? "en"
|
const resolvedLocale = matchSupportedLocale(preferredLocale ?? undefined) ?? detectNavigatorLocale() ?? "en"
|
||||||
globalMessages = await loadLocaleMessages(resolvedLocale)
|
try {
|
||||||
setGlobalRevision((value) => value + 1)
|
globalMessages = await loadLocaleMessages(resolvedLocale)
|
||||||
return resolvedLocale
|
setGlobalRevision((value) => value + 1)
|
||||||
|
return resolvedLocale
|
||||||
|
} catch {
|
||||||
|
globalMessages = enMessages
|
||||||
|
setGlobalRevision((value) => value + 1)
|
||||||
|
return "en"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function tGlobal(key: string, params?: TranslateParams): string {
|
export function tGlobal(key: string, params?: TranslateParams): string {
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
export type UiBootstrapTheme = "light" | "dark" | "system"
|
export type UiBootstrapTheme = "light" | "dark" | "system"
|
||||||
|
|
||||||
export interface UiBootstrapCacheSnapshot {
|
export interface UiBootstrapCacheSnapshot {
|
||||||
theme?: UiBootstrapTheme
|
theme?: UiBootstrapTheme | null
|
||||||
locale?: string
|
locale?: string | null
|
||||||
}
|
}
|
||||||
|
|
||||||
const UI_BOOTSTRAP_CACHE_KEY = "codenomad:ui-bootstrap"
|
const UI_BOOTSTRAP_CACHE_KEY = "codenomad:ui-bootstrap"
|
||||||
@@ -41,7 +41,18 @@ export function writeUiBootstrapCache(snapshot: UiBootstrapCacheSnapshot) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
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 {
|
} catch {
|
||||||
/* noop */
|
/* noop */
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -600,10 +600,17 @@ const configContextValue: ConfigContextValue = {
|
|||||||
|
|
||||||
export const ConfigProvider: ParentComponent = (props) => {
|
export const ConfigProvider: ParentComponent = (props) => {
|
||||||
createEffect(() => {
|
createEffect(() => {
|
||||||
|
if (!isLoaded()) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const bucket = uiConfigBucket()
|
const bucket = uiConfigBucket()
|
||||||
|
const theme = bucket.theme
|
||||||
|
const locale = bucket.settings?.locale
|
||||||
|
|
||||||
writeUiBootstrapCache({
|
writeUiBootstrapCache({
|
||||||
theme: bucket.theme,
|
theme: theme ?? null,
|
||||||
locale: bucket.settings?.locale,
|
locale: locale ?? null,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user