diff --git a/website/package.json b/website/package.json index 3ce195fce69..62d3fd787b9 100644 --- a/website/package.json +++ b/website/package.json @@ -11,7 +11,8 @@ "dev": "npm run generate && npm run clean && next dev", "build": "npm run generate && npm run clean && next build", "postbuild": "node scripts/set-html-lang.js && pagefind --site out --output-path out/_pagefind --exclude-selectors 'noscript,script,style,pre,code,.nextra-code-block,.nextra-toc,.nextra-sidebar,.nextra-breadcrumb,.nextra-banner'", - "preview": "node scripts/preview.js" + "preview": "node scripts/preview.js", + "test": "tsx --test src/**/*.test.ts" }, "keywords": [ "qwen", diff --git a/website/src/components/custom-navbar.tsx b/website/src/components/custom-navbar.tsx index 0265b306848..4889792dea6 100644 --- a/website/src/components/custom-navbar.tsx +++ b/website/src/components/custom-navbar.tsx @@ -9,6 +9,8 @@ import { Button } from "nextra/components"; import { FileText, Star, BookOpen, Newspaper } from "lucide-react"; import { ThemeToggle } from "./theme-toggle"; import { get } from "http"; +import { getBasePath } from "@/lib/blog-utils"; +import { getLocaleFromPathname } from "@/lib/locale-path"; // 定义接口类型 interface NavbarProps { @@ -76,16 +78,11 @@ const getUserLanguage = (): string => { if (typeof window === "undefined") return "en"; // 首先从URL路径中获取语言 - const pathname = window.location.pathname; - const pathSegments = pathname.split("/").filter(Boolean); - - if (pathSegments.length > 0) { - const possibleLang = pathSegments[0]; - const supportedLanguages = ["en", "zh", "de", "fr", "ru", "ja", "pt-BR"]; - if (supportedLanguages.includes(possibleLang)) { - return possibleLang; - } - } + const pathLocale = getLocaleFromPathname( + window.location.pathname, + getBasePath() + ); + if (pathLocale) return pathLocale; // 如果URL中没有语言信息,使用浏览器语言 const browserLang = navigator.language.toLowerCase(); diff --git a/website/src/lib/locale-path.test.ts b/website/src/lib/locale-path.test.ts new file mode 100644 index 00000000000..1901aa5d12c --- /dev/null +++ b/website/src/lib/locale-path.test.ts @@ -0,0 +1,23 @@ +import assert from "node:assert/strict"; +import { describe, it } from "node:test"; +import { getLocaleFromPathname } from "./locale-path"; + +describe("getLocaleFromPathname", () => { + it("finds a locale after the production base path", () => { + assert.equal( + getLocaleFromPathname( + "/qwen-code-docs/fr/users/overview/", + "/qwen-code-docs" + ), + "fr" + ); + }); + + it("finds a locale when the site runs without a base path", () => { + assert.equal(getLocaleFromPathname("/pt-BR/blog/"), "pt-BR"); + }); + + it("returns undefined when the path has no supported locale", () => { + assert.equal(getLocaleFromPathname("/qwen-code-docs/"), undefined); + }); +}); diff --git a/website/src/lib/locale-path.ts b/website/src/lib/locale-path.ts new file mode 100644 index 00000000000..6cf33cb6454 --- /dev/null +++ b/website/src/lib/locale-path.ts @@ -0,0 +1,25 @@ +const SUPPORTED_LOCALES = [ + "en", + "zh", + "de", + "fr", + "ru", + "ja", + "pt-BR", +] as const; + +export type SupportedLocale = (typeof SUPPORTED_LOCALES)[number]; + +/** Returns the route locale after removing an optional deployment base path. */ +export function getLocaleFromPathname( + pathname: string, + basePath = "" +): SupportedLocale | undefined { + const routePath = + basePath && (pathname === basePath || pathname.startsWith(`${basePath}/`)) + ? pathname.slice(basePath.length) + : pathname; + const locale = routePath.split("/").find(Boolean); + + return SUPPORTED_LOCALES.find((supported) => supported === locale); +}