Skip to content

Commit 60d33e5

Browse files
修复顶栏语言切换在英文浏览器下无法进入中文站
语言切换链接始终使用 /zh/ 或 /en/ 前缀,避免经根路径被浏览器偏好重定向回 /en/;同步更新首页平台数据指标。 Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent df05099 commit 60d33e5

3 files changed

Lines changed: 30 additions & 13 deletions

File tree

src/components/HomeStatsSection.astro

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ type StatItem = {
1515
1616
const stats: Record<SiteLang, StatItem[]> = {
1717
zh: [
18-
{ value: "400+", title: "注册用户", icon: "users" },
18+
{ value: "600+", title: "注册用户", icon: "users" },
1919
{ value: "200+亿", title: "Token调用量", icon: "tokens" },
20-
{ value: "500+", title: "容器镜像实例", icon: "containers" },
20+
{ value: "700+", title: "容器镜像实例", icon: "containers" },
2121
{ value: "10+", title: "教学科研奖项", icon: "awards" },
2222
],
2323
en: [
24-
{ value: "400+", title: "Registered users", icon: "users" },
24+
{ value: "600+", title: "Registered users", icon: "users" },
2525
{ value: "20B+", title: "Tokens consumed", icon: "tokens" },
26-
{ value: "500+", title: "Container image instances", icon: "containers" },
26+
{ value: "700+", title: "Container image instances", icon: "containers" },
2727
{ value: "10+", title: "Teaching & research awards", icon: "awards" },
2828
],
2929
};

src/components/Topbar.astro

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@ import iconImage from "@assets/yatcc-icon.png";
44
import ThemeToggleButton from "@components/ThemeToggleButton.astro";
55
import NavDropdown from "@components/NavDropdown.astro";
66
import Link from "@components/Link.astro";
7-
import {
8-
getLangFromUrl,
9-
useTranslations,
10-
useTranslatedPath,
11-
} from "../i18n/utils";
12-
import { languages } from "../i18n/ui";
7+
import { getLangFromUrl, getSwitcherPath, useTranslations } from "../i18n/utils";
8+
import { languages, ui } from "../i18n/ui";
139
import LanguageIcon from "@assets/language.svg";
1410
1511
const lang = getLangFromUrl(Astro.url);
1612
const t = useTranslations(lang);
17-
const translatePath = useTranslatedPath(lang);
1813
1914
interface Props {
2015
logoText?: string;
@@ -63,8 +58,10 @@ const { logoText = "YatCC AI", menuLabel = "Menu" } = Astro.props;
6358
/>
6459
</div>
6560
{
66-
Object.entries(languages).map(([lang, label]) => (
67-
<Link href={translatePath("/", lang)}>{label}</Link>
61+
Object.entries(languages).map(([langCode, label]) => (
62+
<Link href={getSwitcherPath(Astro.url, langCode as keyof typeof ui)}>
63+
{label}
64+
</Link>
6865
))
6966
}
7067
</NavDropdown>

src/i18n/utils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,26 @@ export function getLangFromUrl(url: URL) {
1212
return defaultLang;
1313
}
1414

15+
/** Strip leading /zh or /en from a pathname. */
16+
export function stripLangPrefix(pathname: string): string {
17+
const segments = pathname.split("/").filter(Boolean);
18+
if (segments.length > 0 && segments[0] in ui) {
19+
const rest = segments.slice(1).join("/");
20+
return rest ? `/${rest}` : "/";
21+
}
22+
return pathname || "/";
23+
}
24+
25+
/**
26+
* Path for the language switcher — always uses /zh/... or /en/... so switching
27+
* to the default language does not hit / (browser-language redirect).
28+
*/
29+
export function getSwitcherPath(url: URL, targetLang: keyof typeof ui): string {
30+
const pathWithoutLang = stripLangPrefix(url.pathname);
31+
const suffix = pathWithoutLang === "/" ? "/" : pathWithoutLang;
32+
return `/${targetLang}${suffix}`;
33+
}
34+
1535
export function useTranslations(lang: keyof typeof ui) {
1636
return function t(key: keyof (typeof ui)[typeof defaultLang]) {
1737
return ui[lang][key] || ui[defaultLang][key];

0 commit comments

Comments
 (0)