/** * 双语路径工具:zh 无前缀(历史 URL 1:1),en 加 /en/ 前缀。 * 语言切换 = 同路径跨前缀跳转(预构建双语,毫秒级)。 */ export type Locale = 'zh' | 'en'; export const localeOf = (pathname: string): Locale => pathname === '/en' || pathname.startsWith('/en/') ? 'en' : 'zh'; /** 当前路径在另一语言下的对应路径(zh → /en/...,en → 去 /en 前缀)。 */ export function localePath(pathname: string, to: Locale): string { const clean = pathname.replace(/\/index\.html$/, '').replace(/\/$/, '') || '/'; if (to === 'en') return clean === '/' ? '/en/' : `/en${clean}`; return clean.replace(/^\/en(?=\/|$)/, '') || '/'; }