export const locales = ["en", "zh"] as const; export type Locale = (typeof locales)[number]; export const isLocale = (value: string): value is Locale => (locales as readonly string[]).includes(value); /** Chinese keeps the bare paths; English lives under /en. */ export function localePath(lang: Locale, path: string) { if (lang === "zh") return path; return path === "/" ? "/en" : `/en${path}`; } /** The language a pathname is in. */ export const localeOf = (pathname: string): Locale => pathname === "/en" || pathname.startsWith("/en/") ? "en" : "zh"; /** A pathname without its language prefix. */ export function stripLocale(pathname: string) { if (pathname === "/en") return "/"; return pathname.startsWith("/en/") ? pathname.slice(3) : pathname; } export const htmlLang = { en: "en", zh: "zh-CN" } as const; export const ogLocale = { en: "en_US", zh: "zh_CN" } as const; export const dateLocale = { en: "en-US", zh: "zh-CN" } as const; /** Canonical URL and hreflang alternates for a page that exists in both languages. */ export const alternates = (lang: Locale, path: string) => ({ canonical: localePath(lang, path), languages: { en: localePath("en", path), "zh-CN": path, "x-default": path, }, }); export const formatDate = ( lang: Locale, date: Date | string | number, options: Intl.DateTimeFormatOptions, ) => new Date(date).toLocaleDateString(dateLocale[lang], { timeZone: "UTC", ...options, });