feat(i18n): improve ai chat and settings localization
This commit is contained in:
+19
-2
@@ -2,11 +2,28 @@ import "server-only";
|
||||
|
||||
import { NextResponse } from "next/server";
|
||||
import { normalizeAiError } from "./provider";
|
||||
import type { TranslationValues } from "@/lib/i18n";
|
||||
|
||||
export function aiJsonError(error: unknown): NextResponse {
|
||||
export function aiJsonError(
|
||||
error: unknown,
|
||||
t?: (key: string, values?: TranslationValues) => string,
|
||||
): NextResponse {
|
||||
const normalized = normalizeAiError(error);
|
||||
const reason = typeof normalized.details?.reason === "string"
|
||||
? normalized.details.reason
|
||||
: "provider_unreachable";
|
||||
const translatedReason = t ? t(`finance.ai.errorReasons.${reason}`) : normalized.message;
|
||||
const fallbackReason = translatedReason === `finance.ai.errorReasons.${reason}`
|
||||
? normalized.message
|
||||
: translatedReason;
|
||||
return NextResponse.json(
|
||||
{ error: normalized.message, code: normalized.code },
|
||||
{
|
||||
error: t
|
||||
? t("finance.ai.errorWithReason", { reason: fallbackReason })
|
||||
: normalized.message,
|
||||
code: normalized.code,
|
||||
reason,
|
||||
},
|
||||
{ status: normalized.status },
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user