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

@@ -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<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);
return (
<footer className="border-t border-purple-500/10 py-6 bg-gradient-to-b from-background to-muted/20 backdrop-blur-sm">