| 1 | /// <reference types="vite/client" /> |
| 2 | import { findDefined } from './misc' |
| 3 | |
| 4 | const localeLoaders = import.meta.glob('../../node_modules/dayjs/esm/locale/*.js') |
| 5 | const localePaths = new Map(Object.keys(localeLoaders).map(path => [path.match(/([^/]+)\.js$/)![1], path])) |
| 6 | const localeAliases: Record<string, string> = { zn: 'zh-cn', no: 'nb' } |
| 7 | |
| 8 | function lang2locale(lang: string): string | undefined { |
| 9 | const normalized = lang.toLowerCase() |
| 10 | return localePaths.has(normalized) && normalized |
| 11 | || localeAliases[normalized] |
| 12 | || normalized.includes('-') && lang2locale(normalized.split('-')[0]) |
| 13 | || undefined |
| 14 | } |
| 15 | |
| 16 | export function getLocale() { |
| 17 | return findDefined([navigator.language, ...navigator.languages], lang2locale) |
| 18 | } |
| 19 | |
| 20 | export async function loadLocale() { |
| 21 | const locale = getLocale() |
| 22 | if (!locale) |
| 23 | return |
| 24 | // AdapterDayjs needs the matching dayjs locale module registered before adapterLocale can use it. |
| 25 | await localeLoaders[localePaths.get(locale)!]?.() |
| 26 | return locale |
| 27 | } |