ภาษาของหน้าจอ
LangProvider
Holds the screen language for shared components. An app passes its own locale, for example from next-intl, and handles onChange itself. Without a locale, the language is kept in localStorage, which holds only the preference and never user data.
web และ console มีไฟล์แปลภาษาของตัวเอง (th.json และ en.json ผ่าน next-intl) ข้อความทุกอย่างที่หน้าจอแสดงเอง เช่น หัวคอลัมน์ ป้ายกำกับ และชื่อปุ่ม มาจากไฟล์เหล่านั้น หน้าจอแปลด้วย t() แล้วส่งเป็นข้อความธรรมดาให้คอมโพเนนต์ ครอบแอปด้วย LangProvider ครั้งเดียว โดยส่ง locale ที่ next-intl เลือกไว้ คอมโพเนนต์ใช้ค่านี้กับคำของตัวเอง เช่น หน้าถัดไป ล้างทั้งหมด และป้ายสำหรับโปรแกรมอ่านหน้าจอ และกับรูปแบบวันที่และตัวเลข
import { LangProvider } from "@bsru/ui";ตัวอย่าง
ตั้งค่าครั้งเดียวในแอป
layout ของแอปอ่านภาษาจาก next-intl แล้วส่งให้ LangProvider เมื่อผู้ใช้กดสลับภาษาใน AppShell, onChange เรียก server action ที่ตั้งคุกกี้ NEXT_LOCALE แล้วโหลดหน้าใหม่ ข้อความจากไฟล์แปลและคำของคอมโพเนนต์จึงเปลี่ยนพร้อมกัน
หน้านี้อยู่ใน LangProvider ภาษาตอนนี้คือ th
// src/app/layout.tsx
const locale = (await getLocale()) as Lang;
<NextIntlClientProvider>
<Providers locale={locale}>{children}</Providers>
</NextIntlClientProvider>
// src/app/providers.tsx
"use client";
export function Providers({ locale, children }: { locale: Lang; children: ReactNode }) {
const router = useRouter();
return (
<LangProvider
locale={locale}
onChange={async (next) => {
await setLocale(next); // a server action that sets the NEXT_LOCALE cookie
router.refresh();
}}
>
<ToastProvider>{children}</ToastProvider>
</LangProvider>
);
}| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| locale | "th" | Lang ค่าที่ใช้ได้: "th""en" | The app's own language. Leave it out to let the provider keep the language itself. |
| onChange | async (next) => { await setLocale(next); // a server action that sets the NEXT_LOCALE cookie router.refresh(); } | (next: Lang) => void | Called when someone switches the language, such as in the top bar. |
ข้อความสามแบบ
ข้อความของหน้าจอมาจากไฟล์แปลของแอป ข้อมูลมาจาก API ของ core คำของคอมโพเนนต์และรูปแบบวันที่ตามภาษาของ LangProvider ลองสลับภาษา ทั้งเว็บนี้จะเปลี่ยนตาม
fmtDate("2026-11-02", lang)- 2 พ.ย. 2569
money(150000)- 150,000.00
ปุ่มเลขหน้าและป้ายสำหรับโปรแกรมอ่านหน้าจอเป็นคำของคอมโพเนนต์เอง วันที่เป็นปีพุทธศักราชในภาษาไทย
const t = useTranslations("requests");
<DataTable
caption={t("title")} // 1. the screen's words: from th.json / en.json
columns={[
{ key: "no", label: t("columns.no") },
{ key: "date", label: t("columns.date"), kind: "date" },
]}
rows={rows} // 2. data: from the core API, shown as given
rowKey="no"
/>
// 3. "Next page", "Showing 1-10 of 23" and dates in the Buddhist year come from the component, in the LangProvider language.โดยไม่มี next-intl
สำหรับต้นแบบและเว็บเอกสารนี้ ไม่ต้องส่ง locale แล้ว LangProvider จะเก็บภาษาไว้เอง และจำค่าที่ผู้ใช้เลือกไว้ใน localStorage
หน้านี้อยู่ใน LangProvider ภาษาตอนนี้คือ th
<LangProvider>
<ToastProvider>{children}</ToastProvider>
</LangProvider>props ทั้งหมด
ตัวอย่างด้านบนใช้ props เพียงบางตัว ตารางนี้แสดงทุกตัวที่คอมโพเนนต์รับ พร้อมค่าเริ่มต้นและค่าที่ใช้ได้
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| locale | Lang ค่าที่ใช้ได้: "th""en" | — | The app's own language. Leave it out to let the provider keep the language itself. |
| onChange | (next: Lang) => void | — | Called when someone switches the language, such as in the top bar. |
| children* จำเป็น | ReactNode | — | The app. Every shared component must be inside it. |
ชนิดข้อมูล
Lang
The screen language: th for Thai, en for English.
type Lang = "th" | "en";ข้อควรปฏิบัติ
- ทุก prop ที่เป็นข้อความรับข้อความในภาษาของหน้าจออยู่แล้ว หน้าจอแปลจากไฟล์ของแอปก่อนส่ง ห้ามเขียนข้อความไทยและอังกฤษคู่กันไว้ในหน้าจอ
- ข้อมูลที่เก็บไว้ทั้งสองภาษา เช่น ชื่อไทยและชื่ออังกฤษของหน่วยงาน ให้หน้าจอเลือกตาม locale ก่อนส่ง
- คำของคอมโพเนนต์เองมีทั้งไทยและอังกฤษอยู่ในไลบรารีแล้ว แอปไม่ต้องเพิ่มในไฟล์แปล
- มี LangProvider เพียงตัวเดียวต่อแอป และอยู่เหนือคอมโพเนนต์ทุกตัว
- LangProvider ตั้งค่า lang ของแท็ก html ให้ตรงกับภาษา โปรแกรมอ่านหน้าจอจึงออกเสียงถูก
ซอร์สโค้ด: ui/src/lang.tsx