1
0
mirror of https://github.com/pocket-id/pocket-id.git synced 2026-02-07 07:40:41 +00:00
Files
pocket-id/frontend/src/lib/utils/locale.util.ts
2025-11-04 13:01:12 +00:00

42 lines
1.2 KiB
TypeScript

import {
extractLocaleFromCookie,
setLocale as setParaglideLocale,
type Locale
} from '$lib/paraglide/runtime';
import { setDefaultOptions } from 'date-fns';
import { z } from 'zod/v4';
export async function setLocale(locale: Locale, reload = true) {
await setLocaleForLibraries(locale);
setParaglideLocale(locale, { reload });
document.documentElement.lang = locale;
}
export async function setLocaleForLibraries(
locale: Locale = (extractLocaleFromCookie() as Locale) || 'en'
) {
let dateFnsLocale: string = locale;
if (dateFnsLocale === 'en') {
dateFnsLocale = 'en-US'; // datefns doesn't have 'en'
}
const [zodResult, dateFnsResult] = await Promise.allSettled([
import(`../../../node_modules/zod/v4/locales/${locale}.js`),
import(`../../../node_modules/date-fns/locale/${dateFnsLocale}.js`)
]);
if (zodResult.status === 'fulfilled') {
z.config(zodResult.value.default());
} else {
console.warn(`Failed to load zod locale for ${locale}:`, zodResult.reason);
}
if (dateFnsResult.status === 'fulfilled') {
setDefaultOptions({
locale: dateFnsResult.value.default
});
} else {
console.warn(`Failed to load date-fns locale for ${locale}:`, dateFnsResult.reason);
}
}