From c1f240d007cb9de29841c7481ff655f26fe6826b Mon Sep 17 00:00:00 2001 From: joyzhao Date: Thu, 19 Jun 2025 10:24:48 +0800 Subject: [PATCH] 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 --- src/components/Footer.tsx | 17 +++++++++++++++-- src/components/GlassHeader.tsx | 17 +++++++++++++++-- src/components/LanguageSwitcher.tsx | 29 ++++++++++++++++++++++------- src/layouts/BlogLayout.astro | 4 ++-- src/pages/blog/index.astro | 5 +++++ src/pages/index.astro | 5 +++-- src/pages/projects.astro | 5 +++-- src/pages/zh/blog/index.astro | 5 +++++ src/pages/zh/index.astro | 8 +++++--- src/pages/zh/projects.astro | 5 +++-- 10 files changed, 78 insertions(+), 22 deletions(-) diff --git a/src/components/Footer.tsx b/src/components/Footer.tsx index 47932bc..2b28cf2 100644 --- a/src/components/Footer.tsx +++ b/src/components/Footer.tsx @@ -1,12 +1,25 @@ import { useTranslations, type Lang } from "@/i18n/utils"; import { personalInfo } from "@/lib/data"; import { motion } from "framer-motion"; +import { useState, useEffect } from "react"; +import { defaultLang } from "@/i18n/ui"; interface FooterProps { - lang: Lang; + lang?: Lang; } -export default function Footer({ lang }: FooterProps) { +export default function Footer({ lang: propLang }: FooterProps) { + // 优先使用props传入的语言,如果没有则尝试从HTML lang属性获取 + const [lang, setLang] = useState(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); return (