33 lines
1.5 KiB
TypeScript
33 lines
1.5 KiB
TypeScript
// Locale settings shared by the proxy, server components and client components.
|
|
|
|
export const locales = ["en", "es", "ar"] as const;
|
|
export type Locale = (typeof locales)[number];
|
|
|
|
/** English is served from the root (`/about`); the others carry a prefix (`/es/about`). */
|
|
export const defaultLocale: Locale = "en";
|
|
|
|
export const localeNames: Record<Locale, string> = { en: "English", es: "Español", ar: "العربية" };
|
|
export const localeShort: Record<Locale, string> = { en: "EN", es: "ES", ar: "AR" };
|
|
/** Open Graph locale tags. */
|
|
export const ogLocale: Record<Locale, string> = { en: "en_US", es: "es_ES", ar: "ar_AE" };
|
|
|
|
export const isLocale = (value: string | undefined): value is Locale =>
|
|
(locales as readonly string[]).includes(value ?? "");
|
|
|
|
export const dirOf = (locale: Locale) => (locale === "ar" ? "rtl" : "ltr");
|
|
|
|
/** Prefix an internal path with the locale. Hashes and external URLs pass through untouched. */
|
|
export function localize(path: string, locale: Locale): string {
|
|
if (locale === defaultLocale || !path.startsWith("/")) return path;
|
|
return path === "/" ? `/${locale}` : `/${locale}${path}`;
|
|
}
|
|
|
|
/** Split a browser pathname into its locale and the locale-free path. */
|
|
export function splitLocale(pathname: string): { locale: Locale; path: string } {
|
|
const first = pathname.split("/")[1];
|
|
if (isLocale(first)) {
|
|
return { locale: first, path: pathname.slice(first.length + 1) || "/" };
|
|
}
|
|
return { locale: defaultLocale, path: pathname };
|
|
}
|