feat: add localized home routes
This commit is contained in:
35
src/languageRoutes.ts
Normal file
35
src/languageRoutes.ts
Normal 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 ?? "/";
|
||||
}
|
||||
Reference in New Issue
Block a user