refactor(i18n): simplify language handling across components

use Astro.currentLocale as single source of truth for language
remove manual lang prop passing to components
components now read language from document lang attribute
This commit is contained in:
joyzhao
2025-06-19 10:24:48 +08:00
parent 4621223d26
commit c1f240d007
10 changed files with 78 additions and 22 deletions

View File

@@ -4,12 +4,25 @@ import ThemeToggle from "./ui/theme-toggle";
import { useTranslations, getLocalizedPath, type Lang } from "@/i18n/utils";
import { useState, useEffect } from "react";
import { Menu, X } from "lucide-react";
import { defaultLang } from "@/i18n/ui";
// 从window.document.documentElement.lang获取当前语言
interface GlassHeaderProps {
lang: Lang;
lang?: Lang;
}
export default function GlassHeader({ lang }: GlassHeaderProps) {
export default function GlassHeader({ lang: propLang }: GlassHeaderProps) {
// 优先使用props传入的语言如果没有则尝试从HTML lang属性获取
const [lang, setLang] = useState<Lang>(propLang || defaultLang);
useEffect(() => {
// 在客户端运行时从HTML lang属性获取当前语言
const htmlLang = document.documentElement.lang as Lang;
if (htmlLang && (!propLang || htmlLang !== lang)) {
setLang(htmlLang);
}
}, [propLang, lang]);
const t = useTranslations(lang);
const [isMenuOpen, setIsMenuOpen] = useState(false);
const [isScrolled, setIsScrolled] = useState(false);