修复首页新闻板块未跟随语言的错误

This commit is contained in:
2026-09-01 23:20:41 +08:00
parent 36c58cdfee
commit c9a8ffba33
3 changed files with 8 additions and 7 deletions
+3 -3
View File
@@ -1,8 +1,8 @@
import { HomePageContent } from "@/components/home/home-page-content"; import { HomePageContent } from "@/components/home/home-page-content";
import { getLatestNews } from "@/data/news"; import { getNewsCatalog } from "@/data/news";
export default async function Home() { export default async function Home() {
const news = await getLatestNews(); const newsByLocale = await getNewsCatalog();
return <HomePageContent news={news} />; return <HomePageContent newsByLocale={newsByLocale} />;
} }
+3 -2
View File
@@ -5,7 +5,7 @@ import { useEffect, useState } from "react";
import type { NewsItem } from "@/data/news"; import type { NewsItem } from "@/data/news";
import { applyLocale, getInitialLocale, getLocaleMessages, type Locale } from "@/lib/i18n"; import { applyLocale, getInitialLocale, getLocaleMessages, type Locale } from "@/lib/i18n";
export function HomePageContent({ news }: { news: NewsItem[] }) { export function HomePageContent({ newsByLocale }: { newsByLocale: Record<Locale, NewsItem[]> }) {
const [locale, setLocale] = useState<Locale>("zh-CN"); const [locale, setLocale] = useState<Locale>("zh-CN");
useEffect(() => { useEffect(() => {
@@ -38,6 +38,7 @@ export function HomePageContent({ news }: { news: NewsItem[] }) {
}, []); }, []);
const t = getLocaleMessages(locale); const t = getLocaleMessages(locale);
const visibleNews = newsByLocale[locale] ?? newsByLocale["zh-CN"] ?? [];
return ( return (
<main className="home-page"> <main className="home-page">
@@ -69,7 +70,7 @@ export function HomePageContent({ news }: { news: NewsItem[] }) {
</div> </div>
<div className="news-grid" aria-label={t.home.latest}> <div className="news-grid" aria-label={t.home.latest}>
{news.map((post) => ( {visibleNews.slice(0, 3).map((post) => (
<article key={post.id} className="news-card"> <article key={post.id} className="news-card">
<Link href={post.href} className="news-card-link" aria-label={post.title}> <Link href={post.href} className="news-card-link" aria-label={post.title}>
<div className="news-visual" style={{ backgroundImage: `url(${post.cover})` }} /> <div className="news-visual" style={{ backgroundImage: `url(${post.cover})` }} />
+2 -2
View File
@@ -216,7 +216,7 @@ export async function getNewsBySlug(slug: string, locale: Locale = "zh-CN"): Pro
return items.find((item) => item.slug === slug) ?? null; return items.find((item) => item.slug === slug) ?? null;
} }
export async function getLatestNews() { export async function getLatestNews(locale: Locale = "zh-CN") {
const items = await getNewsList(); const items = await getNewsList(locale);
return items.slice(0, 3); return items.slice(0, 3);
} }