Files
lotteryFront/src/lib/lottery-locale.ts
kang ea75120269 feat: 增强大厅与结果展示功能
- 在 .env.example 中新增可选配置项 NEXT_PUBLIC_LOTTERY_PLAY_CURRENCY
- 在 API 模块中导出 getPlayEffective 函数
- 在 HallScreen 组件中引入 HallPlayCatalogPanel 以展示玩法目录
- 在多个屏幕组件中使用 queueMicrotask 优化数据加载逻辑
- 在 lottery-locale.ts 中新增 getLotteryRequestLocale 函数以支持语言选择
- 在类型定义中新增与玩法相关的类型导出
2026-05-11 10:09:06 +08:00

62 lines
1.7 KiB
TypeScript

import { AxiosHeaders, type AxiosRequestConfig } from "axios";
type LotteryLocale = "zh" | "en" | "ne";
let overrideLocale: LotteryLocale | null = null;
export function setLotteryRequestLocale(locale: string | null): void {
if (locale === null) {
overrideLocale = null;
return;
}
const p = locale.trim().toLowerCase().split("-")[0] ?? "";
overrideLocale = isLotteryLocale(p) ? p : null;
}
function isLotteryLocale(value: string): value is LotteryLocale {
return value === "zh" || value === "en" || value === "ne";
}
/** 供前端展示文案选用语言(与请求头 `X-Locale` 逻辑一致)。 */
export function getLotteryRequestLocale(): LotteryLocale {
return requestLocale();
}
function requestLocale(): LotteryLocale {
if (overrideLocale) {
return overrideLocale;
}
if (typeof document !== "undefined") {
const tag = document.documentElement.lang.trim().toLowerCase();
const primary = tag.split("-")[0] ?? tag;
if (isLotteryLocale(primary)) {
return primary;
}
}
return "en";
}
function acceptLanguage(loc: LotteryLocale): string {
if (loc === "zh") {
return "zh-CN,zh;q=0.9,en;q=0.8";
}
if (loc === "ne") {
return "ne,ne-NP;q=0.9,en;q=0.8";
}
return "en-US,en;q=0.9";
}
export function withLotteryLocaleHeaders(
config: AxiosRequestConfig,
): AxiosRequestConfig {
const loc = requestLocale();
const merged: AxiosRequestConfig = { ...config };
// Axios 的 RequestConfig.headers 类型比 concat 能接受的头对象更窄
const headers = AxiosHeaders.concat(
merged.headers as Parameters<typeof AxiosHeaders.concat>[0],
);
headers.set("X-Locale", loc);
headers.set("Accept-Language", acceptLanguage(loc));
merged.headers = headers;
return merged;
}