返回 CodeWhale
locale-switcher.tsx
根目录 / web / components / locale-switcher.tsx
1 "use client";
2
3 import { useRouter, usePathname } from "next/navigation";
4 import { ALL_LOCALES } from "@/lib/i18n/config";
5 import { fill, getChrome } from "@/lib/i18n/dictionaries";
6 import { replacePathLocale } from "@/lib/i18n/path";
7 import { Icon } from "./icon";
8
9 /** Labels for the dropdown. Keyed by locale code, displayed in native script. */
10 const LOCALE_LABELS: Record<string, string> = {};
11 for (const l of ALL_LOCALES) {
12 LOCALE_LABELS[l.code] = l.label;
13 }
14
15 /** Routed locales that appear in the switcher (shipped + partial). */
16 const ROUTED = ALL_LOCALES.filter((l) => l.status === "shipped" || l.status === "partial");
17
18 export function LocaleSwitcher({ current }: { current: string }) {
19 const router = useRouter();
20 const pathname = usePathname();
21 const chrome = getChrome(current);
22
23 const switchLocale = (code: string) => {
24 if (code === current) return;
25 document.cookie = `NEXT_LOCALE=${code};path=/;max-age=${60 * 60 * 24 * 365}`;
26 const { search, hash } = window.location;
27 router.push(`${replacePathLocale(pathname, code)}${search}${hash}`);
28 };
29
30 // If only 1 routed locale, no switcher needed.
31 if (ROUTED.length <= 1) return null;
32
33 // If exactly 2 routed locales, show a simple toggle.
34 if (ROUTED.length === 2) {
35 const other = ROUTED.find((l) => l.code !== current);
36 if (!other) return null;
37 return (
38 <button
39 type="button"
40 onClick={() => switchLocale(other.code)}
41 className="nav-icon-button"
42 aria-label={fill(chrome.switcherSwitchTo, { label: other.label })}
43 title={other.label}
44 >
45 <Icon name="globe" className="nav-icon" />
46 </button>
47 );
48 }
49
50 // 3+ routed locales: a globe icon over a native <select>, which keeps the
51 // platform picker, keyboard and screen-reader behavior. The select is
52 // transparent and covers the icon, so the whole icon is the hit target.
53 // Partial packs sit under a visible group label so the incomplete scope is
54 // honest at the point of selection. Each option names its language in its
55 // own script, so it carries that language's `lang` (and `dir`); the badge
56 // stays in the page language on the <optgroup> instead of being mixed into
57 // the option text.
58 const option = (l: (typeof ROUTED)[number]) => (
59 <option key={l.code} value={l.code} lang={l.code} dir={l.dir === "rtl" ? "rtl" : undefined}>
60 {l.label}
61 </option>
62 );
63 const partial = ROUTED.filter((l) => l.status === "partial");
64 // The badge is written as an inline "(partial)"; as a group heading it reads
65 // better without its (ASCII or full-width) brackets.
66 const partialHeading = chrome.partialBadge.replace(/^[((]\s*|\s*[))]$/g, "");
67 return (
68 <span className="nav-icon-button nav-locale">
69 <Icon name="globe" className="nav-icon" />
70 <select
71 value={current}
72 onChange={(e) => switchLocale(e.target.value)}
73 aria-label={chrome.switcherLabel}
74 >
75 {ROUTED.filter((l) => l.status !== "partial").map(option)}
76 {partial.length > 0 && <optgroup label={partialHeading}>{partial.map(option)}</optgroup>}
77 </select>
78 </span>
79 );
80 }
81
81 lines Plain Text