Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 7 additions & 10 deletions website/src/components/custom-navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down
23 changes: 23 additions & 0 deletions website/src/lib/locale-path.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
25 changes: 25 additions & 0 deletions website/src/lib/locale-path.ts
Original file line number Diff line number Diff line change
@@ -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);
}