返回 CodeWhale
path.ts
根目录 / web / lib / i18n / path.ts
1 /**
2 * Locale path helpers for the website router.
3 *
4 * Middleware, the locale switcher, and the docs-only theme toggle all have
5 * to agree on what the first path segment means. `pt-BR` is one segment,
6 * not two; a bare `/install` is not yet localized. One implementation keeps
7 * those call sites from drifting into a regex that only matches `[a-z]{2}`.
8 */
9 import { locales } from "./config";
10
11 const ROUTED = locales as readonly string[];
12
13 const LOCALE_PATH_ALIASES = new Map<string, string>([
14 ["zh-cn", "zh"],
15 ["zh-hans", "zh"],
16 ]);
17
18 /**
19 * The routed locale a first path segment names, in canonical casing.
20 *
21 * Matching ignores case so `/pt-br/install` resolves to the same route as
22 * `/pt-BR/install`. Only the regional tag has a case to get wrong, and an
23 * external link that lowercases it used to fall through to the bare-path
24 * branch and land on `/en/pt-br/install` — a 404.
25 */
26 function routedLocale(segment: string | undefined): string | null {
27 if (!segment) return null;
28 const lower = segment.toLowerCase();
29 const aliased = LOCALE_PATH_ALIASES.get(lower);
30 if (aliased) return aliased;
31 return ROUTED.find((l) => l.toLowerCase() === lower) ?? null;
32 }
33
34 function splitPathSuffix(input: string): { path: string; suffix: string } {
35 const queryIndex = input.indexOf("?");
36 const hashIndex = input.indexOf("#");
37 const cut =
38 queryIndex === -1
39 ? hashIndex
40 : hashIndex === -1
41 ? queryIndex
42 : Math.min(queryIndex, hashIndex);
43 if (cut === -1) return { path: input, suffix: "" };
44 return { path: input.slice(0, cut), suffix: input.slice(cut) };
45 }
46
47 /** The routed locale already in `pathname`, or null if the path is bare. */
48 export function pathLocale(pathname: string): string | null {
49 const { path } = splitPathSuffix(pathname);
50 return routedLocale(path.split("/")[1]);
51 }
52
53 /**
54 * Swap or insert the locale prefix. Used by the switcher so a click on
55 * `/pt-BR/docs/guide` lands on `/ja/docs/guide` rather than a nested
56 * `/ja/pt-BR/docs/guide` that the compact nav then treats as a miss, and by
57 * the middleware to fold a miscased prefix onto its canonical spelling.
58 */
59 export function replacePathLocale(pathname: string, locale: string): string {
60 const { path, suffix } = splitPathSuffix(pathname);
61 const segments = path.split("/");
62 if (routedLocale(segments[1])) {
63 segments[1] = locale;
64 return `${segments.join("/")}${suffix}`;
65 }
66 // `"/"` splits to `["", ""]`, so splicing would join to `/<locale>/` — a
67 // URL Next.js only serves after a trailing-slash redirect. Every other
68 // path keeps whatever trailing slash it arrived with.
69 if (path === "" || path === "/") return `/${locale}${suffix}`;
70 segments.splice(1, 0, locale);
71 return `${segments.join("/")}${suffix}`;
72 }
73
74 /** True when the path is a docs route, with or without a locale prefix. */
75 export function isDocsPath(pathname: string): boolean {
76 const { path } = splitPathSuffix(pathname);
77 const segs = path.split("/").filter(Boolean);
78 if (routedLocale(segs[0])) {
79 return segs[1] === "docs";
80 }
81 return segs[0] === "docs";
82 }
83
83 lines TYPESCRIPT