42 lines
998 B
TypeScript
42 lines
998 B
TypeScript
export const LOCALES = ['fr', 'en', 'nl', 'de'] as const;
|
|
export type Locale = typeof LOCALES[number];
|
|
export const DEFAULT_LOCALE: Locale = 'fr';
|
|
|
|
export const LOCALE_NAMES: Record<Locale, string> = {
|
|
fr: 'Français',
|
|
en: 'English',
|
|
nl: 'Nederlands',
|
|
de: 'Deutsch',
|
|
};
|
|
|
|
export const LOCALE_HTML_LANG: Record<Locale, string> = {
|
|
fr: 'fr-BE',
|
|
en: 'en',
|
|
nl: 'nl-BE',
|
|
de: 'de',
|
|
};
|
|
|
|
export const LOCALE_OG: Record<Locale, string> = {
|
|
fr: 'fr_BE',
|
|
en: 'en_US',
|
|
nl: 'nl_BE',
|
|
de: 'de_DE',
|
|
};
|
|
|
|
export function isLocale(value: string): value is Locale {
|
|
return (LOCALES as readonly string[]).includes(value);
|
|
}
|
|
|
|
export function detectLocaleFromAcceptLanguage(header: string | null): Locale {
|
|
if (!header) return DEFAULT_LOCALE;
|
|
const parts = header
|
|
.split(',')
|
|
.map((p) => p.split(';')[0].trim().toLowerCase())
|
|
.filter(Boolean);
|
|
for (const part of parts) {
|
|
const short = part.slice(0, 2);
|
|
if (isLocale(short)) return short;
|
|
}
|
|
return DEFAULT_LOCALE;
|
|
}
|