Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/core/src/locales.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import az from "./locales/az.js";
import en from "./locales/en.js";
import es from "./locales/es.js";
import th from "./locales/th.js";

export { az, es, en };
export { az, es, en, th };
126 changes: 126 additions & 0 deletions packages/core/src/locales/th.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import type { $ZodStringFormats } from "../checks.js";
import type * as errors from "../errors.js";
import * as util from "../util.js";

const Sizable: Record<string, { unit: string; verb: string }> = {
string: { unit: "ตัวอักษร", verb: "ควรมี" },
file: { unit: "ไบต์", verb: "ควรมี" },
array: { unit: "รายการ", verb: "ควรมี" },
set: { unit: "รายการ", verb: "ควรมี" },

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ผมว่าแปลเป็น "เซต" เลยไหมดีครับ

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ผมคิดว่าในบริบทของภาษาอังกฤษ เค้านับ จำนวนสมาชิกในเซต มากกว่าตัวเซตเองนะครับ

const Sizable: Record<string, { unit: string; verb: string }> = {
  string: { unit: "characters", verb: "to have" },
  file: { unit: "bytes", verb: "to have" },
  array: { unit: "items", verb: "to have" },
  set: { unit: "items", verb: "to have" },
};

อย่างในกรณีนี้:

  • ✅ “น้อยกว่ากำหนด: set ควรมีอย่างน้อย 3 รายการ” → สื่อว่ามีสมาชิกไม่ถึง 3 ตัว
  • ❌ “น้อยกว่ากำหนด: set ควรมีอย่างน้อย 3 เซต” → สื่อว่ามี set ซ้อน set ซึ่งไม่ใช่สิ่งที่ schema ต้องการครับ

ผมเลยเสนอให้ใช้คำว่า “รายการ” แทนคำว่า “เซต” เพื่อให้ตรงกับความหมายและไม่ทำให้สับสนครับ 🙏

};

function getSizing(origin: string): { unit: string; verb: string } | null {
return Sizable[origin] ?? null;
}

export const parsedType = (data: any): string => {
const t = typeof data;

switch (t) {
case "number": {
return Number.isNaN(data) ? "ไม่ใช่ตัวเลข (NaN)" : "ตัวเลข";
}
case "object": {
if (Array.isArray(data)) {
return "อาร์เรย์ (Array)";
}
if (data === null) {
return "ไม่มีค่า (null)";
}

if (Object.getPrototypeOf(data) !== Object.prototype && data.constructor) {
return data.constructor.name;
}
}
}
return t;
};

const Nouns: {
[k in $ZodStringFormats | (string & {})]?: string;
} = {
regex: "ข้อมูลที่ป้อน",
email: "ที่อยู่อีเมล",
url: "URL",
emoji: "อิโมจิ",
uuid: "UUID",
uuidv4: "UUIDv4",
uuidv6: "UUIDv6",
nanoid: "nanoid",
guid: "GUID",
cuid: "cuid",
cuid2: "cuid2",
ulid: "ULID",
xid: "XID",
ksuid: "KSUID",
datetime: "วันที่เวลาแบบ ISO",
date: "วันที่แบบ ISO",
time: "เวลาแบบ ISO",
duration: "ช่วงเวลาแบบ ISO",
ipv4: "ที่อยู่ IPv4",
ipv6: "ที่อยู่ IPv6",
cidrv4: "ช่วง IP แบบ IPv4",
cidrv6: "ช่วง IP แบบ IPv6",
base64: "ข้อความแบบ Base64",
base64url: "ข้อความแบบ Base64 สำหรับ URL",
json_string: "ข้อความแบบ JSON",
e164: "เบอร์โทรศัพท์ระหว่างประเทศ (E.164)",
jwt: "โทเคน JWT",
template_literal: "ข้อมูลที่ป้อน",
};

const error: errors.$ZodErrorMap = (issue) => {
switch (issue.code) {
case "invalid_type":
return `ประเภทข้อมูลไม่ถูกต้อง: ควรเป็น ${issue.expected} แต่ได้รับ ${parsedType(issue.input)}`;
case "invalid_value":
if (issue.values.length === 1) return `ค่าไม่ถูกต้อง: ควรเป็น ${util.stringifyPrimitive(issue.values[0])}`;
return `ตัวเลือกไม่ถูกต้อง: ควรเป็นหนึ่งใน ${util.joinValues(issue.values, "|")}`;
case "too_big": {
const adj = issue.inclusive ? "ไม่เกิน" : "น้อยกว่า";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ตรงนี้ถ้าใช้ <= ตามต้นฉบับไปเลยจะชัดเจนกว่าไหมครับ

Copy link
Contributor Author

@mildronize mildronize Apr 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ขอบคุณมากสำหรับคอมเมนต์ดี ๆ เลยครับ 🙏
ตอนแรกผมก็มีคิดในแนวทางเดียวกันเหมือนกัน ว่าการใช้ <= ไปเลยตามต้นฉบับอาจจะชัดกว่า
แต่พอพิจารณาแล้ว ผมเลือกแปลด้วยคำเต็มในภาษาไทยด้วยเหตุผลประมาณนี้ครับ:

  1. ตัวเจ้าของโปรเจกต์ตั้งใจให้เราแปลจริง ๆ
    ใน issue ก็มีข้อความประมาณนี้ไว้เลยครับ

“That PR doesn't update the Nouns map, but you should (to the best of your ability).”

ซึ่งผมตีความว่าเค้าต้องการให้พยายามแปลให้สุดเท่าที่ทำได้ครับ

  1. ภาษาไทยมีหลายระดับตามบริบท
    โดยเฉพาะเวลาเอาไปใช้ในฟอร์ม หรือระบบที่มีผู้ใช้งานทั่วไป (เช่น ภาคราชการ, แอปลงทะเบียน ฯลฯ)
    การใช้ “ไม่เกิน” แทน <= อาจจะทำให้เข้าใจง่ายกว่าในหลายบริบทครับ

  2. ส่วนตัวผมก็ไม่ได้ชอบการแปลมากนักครับ
    ปกติถ้าไปดู repo ส่วนตัวหรือ Blog ผม (Thaitype) จะเห็นว่าใช้ภาษาอังกฤษผสมกับเครื่องหมายพอสมควร
    แต่ในฐานะ contributor ผมพยายามอิงตามแนวทางการแปลแบบ open source ที่เคยมีในวงการ Linux มาก่อน
    แม้มันอาจจะขัดใจนิดนึงในบางจุด แต่ก็อยากลองทำให้มัน “ไทย” จริง ๆ ดูครับ

  3. สุดท้าย locale ภาษาไทยก็ไม่จำเป็นต้องใช้กับทุกเคสครับ
    ผมคิดว่าอันนี้ขึ้นกับกลุ่มผู้ใช้งานมากกว่า
    ถ้า end user คุ้นเคยกับ <= หรือ symbol ทางคณิตอยู่แล้ว ก็คงไม่ยากที่จะใช้ locale อังกฤษไปเลย
    หรือ dev อาจจะ map เป็นคำของตัวเองเพิ่มเติมใน layer อื่นก็ได้ครับ

โดยรวมผมแค่อยากให้ locale นี้ อิงกับการใช้ภาษาไทยแบบจริงจัง เท่าที่จะทำได้
เพื่อให้มันมีพื้นที่สำหรับ use case ที่ภาษาไทยจะช่วยให้สื่อสารชัดขึ้นครับ

แต่ถ้ามี feedback หรือทางเลือกอื่นที่ลื่นกว่านี้ ผมก็ยินดีมากเลยครับที่จะปรับปรุงต่อ 😊

const sizing = getSizing(issue.origin);
if (sizing)
return `เกินกำหนด: ${issue.origin ?? "ค่า"} ควรมี${adj} ${issue.maximum.toString()} ${sizing.unit ?? "รายการ"}`;
return `เกินกำหนด: ${issue.origin ?? "ค่า"} ควรมี${adj} ${issue.maximum.toString()}`;
}
case "too_small": {
const adj = issue.inclusive ? "อย่างน้อย" : "มากกว่า";
const sizing = getSizing(issue.origin);
if (sizing) {
return `น้อยกว่ากำหนด: ${issue.origin} ควรมี${adj} ${issue.minimum.toString()} ${sizing.unit}`;
}

return `น้อยกว่ากำหนด: ${issue.origin} ควรมี${adj} ${issue.minimum.toString()}`;
}
case "invalid_format": {
const _issue = issue as errors.$ZodStringFormatIssues;
if (_issue.format === "starts_with") {
return `รูปแบบไม่ถูกต้อง: ข้อความต้องขึ้นต้นด้วย "${_issue.prefix}"`;
}
if (_issue.format === "ends_with") return `รูปแบบไม่ถูกต้อง: ข้อความต้องลงท้ายด้วย "${_issue.suffix}"`;
if (_issue.format === "includes") return `รูปแบบไม่ถูกต้อง: ข้อความต้องมี "${_issue.includes}" อยู่ในข้อความ`;
if (_issue.format === "regex") return `รูปแบบไม่ถูกต้อง: ต้องตรงกับรูปแบบที่กำหนด ${_issue.pattern}`;
return `รูปแบบไม่ถูกต้อง: ${Nouns[_issue.format] ?? issue.format}`;
}
case "not_multiple_of":
return `ตัวเลขไม่ถูกต้อง: ต้องเป็นจำนวนที่หารด้วย ${issue.divisor} ได้ลงตัว`;
case "unrecognized_keys":
return `พบคีย์ที่ไม่รู้จัก: ${util.joinValues(issue.keys, ", ")}`;
case "invalid_key":
return `คีย์ไม่ถูกต้องใน ${issue.origin}`;
case "invalid_union":
return "ข้อมูลไม่ถูกต้อง: ไม่ตรงกับรูปแบบยูเนียนที่กำหนดไว้";
case "invalid_element":
return `ข้อมูลไม่ถูกต้องใน ${issue.origin}`;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ผมว่าใช้ "องค์ประกอบไม่ถูกต้อง" หรือ "อีลีเมนต์ไม่ถูกต้อง" ก็ได้ครับ

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ดูจาก Test Cases การใช้งาน ผมคิดว่า คำว่า "ข้อมูลไม่ถูกต้องใน ..." น่าจะเหมาะสมกว่า แต่กว้างคลอบคลุมทุกบริบทกว่าคับ

test("z.map invalid_element", () => {
  const a = z.map(z.bigint(), z.number());
  const r1 = z.safeParse(a, new Map([[BigInt(123), BigInt(123)]]));

  expect(r1.error!.issues[0].code).toEqual("invalid_element");
  expect(r1.error!.issues[0].path).toEqual([]);
});

เพราะ "ข้อมูลบางชิ้นในโครงสร้าง" ที่ไม่ผ่านการตรวจสอบ
เช่นใน Map, Set, Array หรือ Tuple ที่องค์ประกอบ (element) บางตัวไม่ตรงกับ schema

เช่น "ข้อมูลไม่ถูกต้องใน map" ฟังดูเป็นกลาง ไม่สับสน ไม่แข็งเกินไป

default:
return `ข้อมูลไม่ถูกต้อง`;
}
};

export { error };

export default function (): { localeError: errors.$ZodErrorMap } {
return {
localeError: error,
};
}
1 change: 1 addition & 0 deletions packages/docs/content/error-customization.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,4 @@ The following locales are available:

- `az` — Azerbaijani
- `en` — English
- `th` — Thai