feat(i18n): 更新中英文翻译及国际化支持

- 在Workshop和Line模块中添加新的翻译字段
- 修复页面中硬编码的文本,改为使用翻译
- 更新产品线和车间展示的英文翻译内容
- 为卡片组件添加locale属性传递
This commit is contained in:
砂糖
2026-02-03 17:18:13 +08:00
parent 86c20e5ef7
commit e7a428d8f0
7 changed files with 65 additions and 44 deletions

View File

@@ -4,6 +4,7 @@ import { constructMetadata } from "@/lib/metadata";
import { Line } from "@/types/line";
import { Metadata } from "next";
import { Locale } from "next-intl";
import { getTranslations } from "next-intl/server";
// 强制静态渲染
export const dynamic = "force-static";
@@ -48,6 +49,7 @@ export async function generateMetadata({
export default async function ProductDetailPage({ params }: { params: Params }) {
const { locale, slug } = await params;
const line = await getLine(locale, slug);
const t = await getTranslations({ locale, namespace: "Line" });
if (!line) return <div className="p-6">404 - 线</div>;
@@ -82,7 +84,7 @@ export default async function ProductDetailPage({ params }: { params: Params })
{line.images && line.images.length > 0 && (
<div className="mb-12">
<h2 className="text-2xl md:text-3xl font-semibold text-gray-800 mb-6">
{t("productImages")}
</h2>
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
{line.images.map((imgUrl, index) => (
@@ -103,7 +105,7 @@ export default async function ProductDetailPage({ params }: { params: Params })
{line.properties && line.properties.length > 0 && (
<div className="mb-12">
<h2 className="text-2xl md:text-3xl font-semibold text-gray-800 mb-6">
{t("productProperties")}
</h2>
<ul className="space-y-4">
{line.properties.map((prop, index) => (

View File

@@ -110,7 +110,7 @@ export default async function Page({
}) {
const { locale } = await params;
// 获取翻译(确保服务端/客户端一致)
const t = await getTranslations({ locale, namespace: "Workshop" });
const t = await getTranslations({ locale, namespace: "Line" });
// 获取产品线数据(顶层 awaitServer Component 原生支持)
const products: Line[] = await getLines(locale);
@@ -129,7 +129,7 @@ export default async function Page({
{products.map((product) => {
// 用稳定的 key优先用唯一标识如 id无 id 则用 title+locale 避免 index 导致的问题)
const stableKey = `${product.title}-${product.locale}`;
return <ProductCard key={stableKey} product={product} />;
return <ProductCard key={stableKey} product={{ ...product, locale }} />;
})}
</div>
) : (

View File

@@ -4,6 +4,7 @@ import { getWorkShop, getWorkShops } from "@/lib/workshop";
import { WorkShop } from "@/types/workShop";
import { Metadata } from "next";
import { Locale } from "next-intl";
import { getTranslations } from "next-intl/server";
// 固定 Params 类型为普通对象Next.js 原生传参无异步)
type Params = {
@@ -45,6 +46,7 @@ export async function generateMetadata({
export default async function WorkshopDetailPage({ params }: { params: Params }) {
const { locale, slug } = await params;
const workShop = await getWorkShop(locale, slug);
const t = await getTranslations({ locale, namespace: "Workshop" });
if (!workShop) return null;
@@ -78,7 +80,7 @@ export default async function WorkshopDetailPage({ params }: { params: Params })
{workShop.images?.length > 0 && (
<div className="bg-white rounded-xl p-8 shadow-xl mt-12">
<h2 className="text-2xl font-semibold text-gray-800 mb-6 border-b pb-3 border-gray-200">
{t("pageImages")}
</h2>
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
{workShop.images.map((img, index) => {

View File

@@ -93,7 +93,7 @@ export default async function Page({ params }: { params: Params }) {
{workShops.length > 0 ? (
workShops.map((workshop) => {
const stableKey = `${workshop.title}-${workshop.locale}`;
return <WorkshopCard key={stableKey} workshop={workshop} />;
return <WorkshopCard key={stableKey} workshop={{ ...workshop, locale }} />;
})
) : (
<EmptyState />