Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/app/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,36 @@ export type Lang = keyof typeof langs
export type RawDictionary = typeof en.dict
export type Dictionary = i18n.Flatten<RawDictionary>

// Fetch and flatten the dictionary
// English dictionary cache for fallback
let enDictCache: Dictionary | null = null

const fetchEnDict = async (): Promise<Dictionary> => {
if (!enDictCache) {
const dict: RawDictionary = (await import("~/lang/en/entry")).dict
enDictCache = i18n.flatten(dict)
}
return enDictCache
}

// Fetch and flatten the dictionary, with English fallback
const fetchDictionary = async (locale: Lang): Promise<Dictionary> => {
try {
const dict: RawDictionary = (await import(`~/lang/${locale}/entry.ts`)).dict
return i18n.flatten(dict) // Flatten dictionary for easier access to keys
const flatDict = i18n.flatten(dict)

// If not English, merge with English as fallback (English keys underneath, locale on top)
if (locale !== "en") {
const enDict = await fetchEnDict()
return { ...enDict, ...flatDict } as Dictionary
}

return flatDict
} catch (err) {
console.error(`Error loading dictionary for locale: ${locale}`, err)
// Fallback to English if the requested locale fails to load
if (locale !== "en") {
return await fetchEnDict()
}
throw new Error(`Failed to load dictionary for ${locale}`)
}
}
Expand Down