i18n: - LangProvider (client context) + cookie/lang localStorage sync - locales/th.json + en.json จาก design (269 keys) - แปล header/nav/footer/home/route/stop/check ผ่าน t() - server pages อ่าน cookie lang (SSR แปลตรง) + client context Stop search (หน้า /stops): - code search 5 หลัก (kilometer '36+020' → pad5 '36020') + numpad - place search (ชื่อสถานที่) + suggest - near-me (geolocation → ป้ายทางหลวงใกล้สุด 8 อัน, haversine) - data: busstop_list.json (154 ป้ายทางหลวง) Route detail: km list ใช้ระยะจริงจาก stop-km.json (แทน stop_sequence) Note: 'nearby ใกล้เคียงป้าย' ไม่ทำ — stations.json ไม่มี landmarks data
32 lines
936 B
TypeScript
32 lines
936 B
TypeScript
import th from "@/data/locales/th.json";
|
|
import en from "@/data/locales/en.json";
|
|
|
|
export type Lang = "th" | "en";
|
|
|
|
const DICTS: Record<Lang, Record<string, string>> = {
|
|
th: th as Record<string, string>,
|
|
en: en as Record<string, string>,
|
|
};
|
|
|
|
/** แปลข้อความตามคีย์ (จาก design locales/th.json + en.json) */
|
|
export function t(
|
|
key: string,
|
|
lang: Lang,
|
|
vars?: Record<string, string | number>,
|
|
): string {
|
|
let s = DICTS[lang]?.[key] ?? DICTS.th[key] ?? key;
|
|
if (vars) {
|
|
for (const [k, v] of Object.entries(vars)) {
|
|
s = s.replace(new RegExp(`\\{${k}\\}`, "g"), String(v));
|
|
}
|
|
}
|
|
return s;
|
|
}
|
|
|
|
/** อ่าน lang จากคุกกี้ (server-side) */
|
|
export function langFromCookie(cookieHeader: string | null | undefined): Lang {
|
|
if (!cookieHeader) return "th";
|
|
const m = cookieHeader.match(/(?:^|;\s*)lang=(th|en)\b/);
|
|
return m ? (m[1] as Lang) : "th";
|
|
}
|