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">

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);

View File

@@ -1,5 +1,5 @@
import { useState, useEffect } from "react";
import { getLocalizedPath, type Lang } from "@/i18n/utils"; // getLangFromUrl is removed as Astro.currentLocale is used now.
import { getLocalizedPath, type Lang } from "@/i18n/utils";
import { languages as i18nLanguages, defaultLang } from "@/i18n/ui";
import { Languages, Check, ChevronDown } from "lucide-react";
import { motion, AnimatePresence } from "framer-motion";
@@ -7,26 +7,41 @@ import { motion, AnimatePresence } from "framer-motion";
const availableLanguages = Object.entries(i18nLanguages).map(([code, name]) => ({
code: code as Lang,
name,
// You can add icons here if you have a mapping or a more complex structure in ui.ts
// 语言图标映射
icon: code === 'en' ? '🇬🇧' : code === 'zh' ? '🇨🇳' : '🌐'
}));
interface LanguageSwitcherProps {
lang: Lang;
lang?: Lang;
}
export default function LanguageSwitcher({ lang: initialLang }: LanguageSwitcherProps) {
export default function LanguageSwitcher({ lang: propLang }: LanguageSwitcherProps) {
const [isOpen, setIsOpen] = useState(false);
// 获取当前语言优先使用props传入的语言
const [currentLang, setCurrentLang] = useState<Lang>(propLang || defaultLang);
// 在客户端运行时从HTML lang属性获取当前语言
useEffect(() => {
if (typeof document !== 'undefined') {
const htmlLang = document.documentElement.lang as Lang;
if (htmlLang && (!propLang || htmlLang !== currentLang)) {
setCurrentLang(htmlLang);
}
}
}, [propLang, currentLang]);
const [selectedLanguage, setSelectedLanguage] = useState(() => {
return availableLanguages.find(l => l.code === initialLang) || availableLanguages.find(l => l.code === defaultLang) || availableLanguages[0];
return availableLanguages.find(l => l.code === currentLang) ||
availableLanguages.find(l => l.code === defaultLang) ||
availableLanguages[0];
});
useEffect(() => {
const currentLangObject = availableLanguages.find(l => l.code === initialLang);
const currentLangObject = availableLanguages.find(l => l.code === currentLang);
if (currentLangObject && currentLangObject.code !== selectedLanguage.code) {
setSelectedLanguage(currentLangObject);
}
}, [initialLang, selectedLanguage.code]);
}, [currentLang, selectedLanguage.code]);
const toggleOpen = () => setIsOpen(!isOpen);