feat: add localized home routes

This commit is contained in:
TerryM
2026-06-01 15:09:58 +08:00
parent 9b08379d50
commit fa78568c94
8 changed files with 144 additions and 18 deletions

35
src/languageRoutes.ts Normal file
View File

@@ -0,0 +1,35 @@
import type { Lang } from "./i18n";
export const localizedHomeRoutes: ReadonlyArray<{ lang: Lang; path: string }> =
[
{ lang: "zh-CN", path: "/chinese" },
{ lang: "ja", path: "/japanese" },
{ lang: "ko", path: "/korean" },
{ lang: "vi", path: "/vietnamese" },
{ lang: "id", path: "/indonesian" },
{ lang: "ms", path: "/malay" },
];
function normalizePathname(pathname: string): string {
const normalized = pathname.replace(/\/+$/, "");
return normalized || "/";
}
export function languageForHomePathname(pathname: string): Lang | null {
const normalized = normalizePathname(pathname);
return (
localizedHomeRoutes.find((route) => route.path === normalized)?.lang ?? null
);
}
export function isHomePathname(pathname: string): boolean {
return (
normalizePathname(pathname) === "/" ||
languageForHomePathname(pathname) !== null
);
}
export function homePathForLang(lang: Lang): string {
if (lang === "en") return "/";
return localizedHomeRoutes.find((route) => route.lang === lang)?.path ?? "/";
}