Files
manoon-headless/src/lib/i18n/locales.ts

38 lines
1.3 KiB
TypeScript

export const SUPPORTED_LOCALES = ["sr", "en", "de", "fr"] as const;
export type Locale = (typeof SUPPORTED_LOCALES)[number];
export const DEFAULT_LOCALE: Locale = "sr";
export const LOCALE_COOKIE = "NEXT_LOCALE";
export const LOCALE_CONFIG: Record<Locale, { label: string; flag: string; saleorLocale: string }> = {
sr: { label: "Srpski", flag: "🇷🇸", saleorLocale: "SR" },
en: { label: "English", flag: "🇬🇧", saleorLocale: "EN" },
de: { label: "Deutsch", flag: "🇩🇪", saleorLocale: "EN" },
fr: { label: "Français", flag: "🇫🇷", saleorLocale: "EN" },
};
export function isValidLocale(locale: string): locale is Locale {
return SUPPORTED_LOCALES.includes(locale as Locale);
}
export function getSaleorLocale(locale: Locale): string {
return LOCALE_CONFIG[locale].saleorLocale;
}
export function getLocaleFromPath(pathname: string): string {
const pattern = SUPPORTED_LOCALES.join("|");
const match = pathname.match(new RegExp(`^\\/(${pattern})`));
return match ? match[1] : DEFAULT_LOCALE;
}
export function getPathWithoutLocale(pathname: string): string {
const pattern = SUPPORTED_LOCALES.join("|");
return pathname.replace(new RegExp(`^\\/(${pattern})`), "") || "/";
}
export function buildLocalePath(locale: Locale, path: string): string {
const pathPart = path === "/" ? "" : path;
return `/${locale}${pathPart}`;
}