-
Notifications
You must be signed in to change notification settings - Fork 45
feat(i18n): Header 加匿名可用的 ZH/EN 语言切换按钮 #300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,85 @@ | ||||||||||||||||||||||||||||||||||||||||||
| "use client"; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||
| * Header 里的语言切换按钮(匿名也能用)。 | ||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||
| * 为什么要做: | ||||||||||||||||||||||||||||||||||||||||||
| * 之前切语言的唯一入口在 /settings 页面,UserMenu 里只有登录用户能看到。 | ||||||||||||||||||||||||||||||||||||||||||
| * 访客看到的永远是默认 zh,站点对英语用户非常不友好。 | ||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||
| * 实现: | ||||||||||||||||||||||||||||||||||||||||||
| * - 写 locale=zh|en 到 document.cookie(path=/,一年有效期,samesite=lax) | ||||||||||||||||||||||||||||||||||||||||||
| * 字段和格式与 SettingsForm 完全一致,登录用户在设置页改的偏好仍然生效 | ||||||||||||||||||||||||||||||||||||||||||
| * - 切完 router.refresh() 让 SSR 重新渲染,server component(Hero / docs | ||||||||||||||||||||||||||||||||||||||||||
| * 详情页等)从 cookie 读新 locale 切文案 | ||||||||||||||||||||||||||||||||||||||||||
| * - 简单的 ZH / EN 双字母展示,当前语言高亮;button 尺寸与 ThemeToggle 对齐 | ||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||
| * 不做的事: | ||||||||||||||||||||||||||||||||||||||||||
| * - 不读后端 user_preferences:游客无账号,登录用户在 /settings 改完也只是 | ||||||||||||||||||||||||||||||||||||||||||
| * 写同一条 cookie,这里读 cookie 即可统一视图。 | ||||||||||||||||||||||||||||||||||||||||||
| * - 不做 URL prefix 切换(/zh/xxx、/en/xxx):站点当前没有 i18n 路由分段, | ||||||||||||||||||||||||||||||||||||||||||
| * 硬改会牵动大量路由和 sitemap。Cookie 方案延续现状,后续若要 SEO 双语 | ||||||||||||||||||||||||||||||||||||||||||
| * URL 再另开 PR。 | ||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| import { useEffect, useState } from "react"; | ||||||||||||||||||||||||||||||||||||||||||
| import { useRouter } from "next/navigation"; | ||||||||||||||||||||||||||||||||||||||||||
| import { Button } from "../../components/ui/button"; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| type Locale = "zh" | "en"; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| function readLocaleCookie(): Locale { | ||||||||||||||||||||||||||||||||||||||||||
| if (typeof document === "undefined") return "zh"; | ||||||||||||||||||||||||||||||||||||||||||
| const m = document.cookie.match(/(?:^|;\s*)locale=([^;]+)/); | ||||||||||||||||||||||||||||||||||||||||||
| const v = m?.[1]; | ||||||||||||||||||||||||||||||||||||||||||
| return v === "en" ? "en" : "zh"; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| function writeLocaleCookie(next: Locale) { | ||||||||||||||||||||||||||||||||||||||||||
| // 一年;samesite=lax 够用(这个 cookie 不涉及跨站 POST) | ||||||||||||||||||||||||||||||||||||||||||
| document.cookie = `locale=${next};path=/;max-age=${60 * 60 * 24 * 365};samesite=lax`; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| export function LocaleToggle() { | ||||||||||||||||||||||||||||||||||||||||||
| const router = useRouter(); | ||||||||||||||||||||||||||||||||||||||||||
| // 初始 render 先给默认值,避免 SSR 和 CSR 结构不同触发 hydration 警告; | ||||||||||||||||||||||||||||||||||||||||||
| // 真实值由 useEffect 读 cookie 后再覆盖 | ||||||||||||||||||||||||||||||||||||||||||
| const [locale, setLocale] = useState<Locale>("zh"); | ||||||||||||||||||||||||||||||||||||||||||
| const [ready, setReady] = useState(false); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||||||||||||||||||||||||||
| setLocale(readLocaleCookie()); | ||||||||||||||||||||||||||||||||||||||||||
| setReady(true); | ||||||||||||||||||||||||||||||||||||||||||
| }, []); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| const toggle = () => { | ||||||||||||||||||||||||||||||||||||||||||
| const next: Locale = locale === "zh" ? "en" : "zh"; | ||||||||||||||||||||||||||||||||||||||||||
| writeLocaleCookie(next); | ||||||||||||||||||||||||||||||||||||||||||
| setLocale(next); | ||||||||||||||||||||||||||||||||||||||||||
| if (typeof window !== "undefined" && window.umami) { | ||||||||||||||||||||||||||||||||||||||||||
| window.umami.track("locale_toggle", { locale: next }); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| // 刷新 server component 树,重新按 cookie 渲染各页面 | ||||||||||||||||||||||||||||||||||||||||||
| router.refresh(); | ||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||
| <Button | ||||||||||||||||||||||||||||||||||||||||||
| variant="ghost" | ||||||||||||||||||||||||||||||||||||||||||
| size="sm" | ||||||||||||||||||||||||||||||||||||||||||
| onClick={toggle} | ||||||||||||||||||||||||||||||||||||||||||
| aria-label="Toggle language" | ||||||||||||||||||||||||||||||||||||||||||
| title={locale === "zh" ? "切换为 English" : "Switch to 中文"} | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+66
to
+72
|
||||||||||||||||||||||||||||||||||||||||||
| return ( | |
| <Button | |
| variant="ghost" | |
| size="sm" | |
| onClick={toggle} | |
| aria-label="Toggle language" | |
| title={locale === "zh" ? "切换为 English" : "Switch to 中文"} | |
| const title = !ready | |
| ? "Toggle language" | |
| : locale === "zh" | |
| ? "切换为 English" | |
| : "Switch to 中文"; | |
| return ( | |
| <Button | |
| variant="ghost" | |
| size="sm" | |
| onClick={toggle} | |
| aria-label="Toggle language" | |
| title={title} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里的 locale cookie 读取/写入逻辑(正则、默认值、max-age / samesite 等)与
app/settings/SettingsForm.tsx重复;两处都宣称“格式完全一致”,后续任一处改动容易产生 drift。建议抽到一个共享 helper(如lib/locale-cookie.ts),由 SettingsForm / LocaleToggle 共同复用。