Skip to content

Commit 6ff724d

Browse files
committed
修正中文页面切换回英文的语言切换逻辑
siteConfig.baseUrl 在非默认 locale 下已包含 locale 前缀, 需要先剥离 locale 再拼接路径。
1 parent 843052c commit 6ff724d

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

‎src/theme/Navbar/index.tsx‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@ function Navbar(): React.ReactElement {
2121

2222
const getAlternateLocalePath = useCallback(() => {
2323
const pathname = location.pathname;
24-
const { baseUrl } = siteConfig;
24+
// siteConfig.baseUrl includes locale for non-default locales (e.g. /docs/zh-CN/)
25+
// Strip locale to get the bare base (e.g. /docs/)
26+
const bareBase = siteConfig.baseUrl.replace(/zh-CN\/$/, "");
27+
const pathAfterBare = pathname.slice(bareBase.length);
2528
if (currentLocale === "en") {
2629
// /docs/some-page -> /docs/zh-CN/some-page
27-
const pathAfterBase = pathname.slice(baseUrl.length);
28-
return `${baseUrl}zh-CN/${pathAfterBase}`;
30+
return `${bareBase}zh-CN/${pathAfterBare}`;
2931
}
3032
// /docs/zh-CN/some-page -> /docs/some-page
31-
const pathAfterBase = pathname.slice(baseUrl.length);
32-
const withoutLocale = pathAfterBase.replace(/^zh-CN\/?/, "");
33-
return `${baseUrl}${withoutLocale}`;
33+
const withoutLocale = pathAfterBare.replace(/^zh-CN\/?/, "");
34+
return `${bareBase}${withoutLocale}`;
3435
}, [currentLocale, location.pathname, siteConfig]);
3536

3637
const toggleColorMode = useCallback(() => {

0 commit comments

Comments
 (0)