fix: pass the locale through the frontend chat (#668)

* fix: pass the locale through the frontend chat

* update the code as the review suggestion

* update the code to fix the lint error
This commit is contained in:
Willem Jiang
2025-10-28 08:41:54 +08:00
committed by GitHub
parent b4c09aa4b1
commit eb4c3b8ef6

View File

@@ -12,6 +12,31 @@ import { sleep } from "../utils";
import { resolveServiceURL } from "./resolve-service-url";
import type { ChatEvent } from "./types";
function getLocaleFromCookie(): string {
if (typeof document === "undefined") return "en-US";
// Map frontend locale codes to backend locale format
// Frontend uses: "en", "zh"
// Backend expects: "en-US", "zh-CN"
const LOCALE_MAP = { "en": "en-US", "zh": "zh-CN" } as const;
// Initialize to raw locale format (matches cookie format)
let rawLocale = "en";
// Read from cookie
const cookies = document.cookie.split(";");
for (const cookie of cookies) {
const [name, value] = cookie.trim().split("=");
if (name === "NEXT_LOCALE" && value) {
rawLocale = decodeURIComponent(value);
break;
}
}
// Map raw locale to backend format, fallback to en-US if unmapped
return LOCALE_MAP[rawLocale as keyof typeof LOCALE_MAP] ?? "en-US";
}
export async function* chatStream(
userMessage: string,
params: {
@@ -47,9 +72,11 @@ export async function* chatStream(
return yield* chatReplayStream(userMessage, params, options);
try{
const locale = getLocaleFromCookie();
const stream = fetchStream(resolveServiceURL("chat/stream"), {
body: JSON.stringify({
messages: [{ role: "user", content: userMessage }],
locale,
...params,
}),
signal: options.abortSignal,