Files
2026-04-18 23:52:15 +08:00

49 lines
1.4 KiB
TypeScript

import {NextRequest, NextResponse} from "next/server";
import {getTranslation, getTranslations} from "@/lib/database";
// 单个翻译查询
export async function GET(request: NextRequest) {
const {searchParams} = new URL(request.url);
const chineseName = searchParams.get("name");
if (!chineseName) {
return NextResponse.json({error: "缺少 name 参数"}, {status: 400});
}
const translation = await getTranslation(chineseName);
return NextResponse.json({
chinese: chineseName,
english: translation,
found: translation !== null,
});
}
// 批量翻译查询
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const {names} = body as { names: string[] };
if (!names || !Array.isArray(names)) {
return NextResponse.json({error: "缺少 names 参数(数组)"}, {status: 400});
}
const translations = await getTranslations(names);
const result: Record<string, string | null> = {};
for (const name of names) {
result[name] = translations.get(name) || null;
}
return NextResponse.json({
translations: result,
total: names.length,
found: translations.size,
});
} catch (error) {
console.error("Translation API error:", error);
return NextResponse.json({error: "请求格式错误"}, {status: 400});
}
}