This commit is contained in:
eric
2026-03-11 23:17:16 -05:00
parent efe816915d
commit 51b88291ef
5 changed files with 16 additions and 23 deletions

View File

@@ -85,15 +85,18 @@ export function useTranslation(namespace?: string) {
};
}
function navigate(pathname: string, locale: Locale, opts?: { locale?: Locale }) {
const newLocale = opts?.locale ?? locale;
const cleanPath = pathname.replace(/^\/(zh|en)/, "") || "/";
const newPath = `/${newLocale}${cleanPath === "/" ? "" : cleanPath}`;
window.location.href = newPath;
}
export function useLocaleRouter() {
const locale = useLocale();
return {
replace: (pathname: string, opts?: { locale?: Locale }) => {
const newLocale = opts?.locale ?? locale;
const cleanPath = pathname.replace(/^\/(zh|en)/, "") || "/";
const newPath = `/${newLocale}${cleanPath === "/" ? "" : cleanPath}`;
window.location.href = newPath;
},
push: (pathname: string, opts?: { locale?: Locale }) => navigate(pathname, locale, opts),
replace: (pathname: string, opts?: { locale?: Locale }) => navigate(pathname, locale, opts),
};
}