Skip to main content

กฎพหูพจน์ CLDR : ตรวจหมวดหมู่ i18n ข้ามภาษา

อังกฤษมีพหูพจน์ 2 รูป อาหรับ 6 และรัสเซีย 4 หาก i18n รองรับเพียง 'one' กับ 'other' แอปจะเสียในภาษาส่วนใหญ่ คู่มือนี้อธิบายกฎ CLDR และวิธีตรวจ

1

กฎพหูพจน์ CLDR คืออะไร?

Unicode CLDR (Common Locale Data Repository) กำหนดหกหมวดหมู่คือ zero, one, two, few, many, other แต่ละภาษาใช้บางหมวดหมู่พร้อมกฎตัวเลขเฉพาะ อังกฤษใช้ one กับ 1 และ other กับที่เหลือ แต่ภาษาส่วนใหญ่ซับซ้อนกว่า การใช้ผิดทำให้ข้อความผิดไวยากรณ์และแอปดูไม่เป็นมืออาชีพ

The plural problem
// English has 2 plural forms: one, other
// "1 item" vs "2 items"

// But many languages have more:
// Arabic: 6 forms (zero, one, two, few, many, other)
// Polish: 3 forms (one, few, other)
// Japanese: 1 form (other)
// Czech: 3 forms (one, few, other)

// If you only provide "one" and "other",
// Arabic and Polish users see broken text.
หมวดหมู่ 'other' จำเป็นในทุกภาษา เป็นค่าสำรองเมื่อไม่มีกฎอื่นตรง หากกำหนดเพียง 'one' กับ 'other' จะถูกในอังกฤษแต่เสียในภาษาทั่วโลกมากกว่า 60%
2

หมวดหมู่พหูพจน์ทั้งหก

CLDR กำหนดหกหมวดหมู่ ไม่ใช่ทุกภาษาใช้ทั้งหมด อังกฤษใช้สอง แต่อาหรับใช้ครบ ไฟล์ i18n ต้องกำหนดหมวดหมู่ที่ภาษาเป้าหมายต้องใช้ มิฉะนั้นผู้ใช้จะเห็นแอปหยุด คีย์ดิบ หรือไวยากรณ์ผิด

CLDR plural categories
// CLDR defines 6 plural categories:
// zero  - 0 items (Arabic, Latvian)
// one   - 1 item (most languages)
// two   - 2 items (Arabic, Welsh)
// few   - 2-4 items (Polish, Czech, Russian)
// many  - 5-19 items (Arabic, Polish, Russian)
// other - everything else (required for ALL languages)

// Examples by language:
// English:  one, other                    (2 forms)
// French:   one, many, other              (3 forms)
// Arabic:   zero, one, two, few, many, other  (6 forms)
// Japanese: other                         (1 form)
// Polish:   one, few, many, other         (4 forms)
// Russian:  one, few, many, other         (4 forms)
3

หมวดหมู่พหูพจน์ตามภาษา

ข้อมูลอ้างอิงอย่างรวดเร็วสำหรับหมวดหมู่ที่ภาษายอดนิยมต้องใช้ ใช้เมื่อกำหนดรูปพหูพจน์ในไฟล์แปล

Common mistakes
// MISTAKE 1: Only providing "one" and "other"
// en.json
{
  "items_one": "{{count}} item",
  "items_other": "{{count}} items"
}
// This breaks for Arabic (missing zero, two, few, many)

// MISTAKE 2: Hardcoded plural logic
// WRONG:
const text = count === 1 ? "1 item" : `${count} items`
// This fails for languages where "1" isn't the only "one" form

// MISTAKE 3: Missing "other" category
// "other" is REQUIRED for every language
// Without it, some numbers show raw keys

// MISTAKE 4: Translating plural RULES instead of text
// The CLDR rules (one, few, many) are universal
// Only the TEXT after each rule should be translated
4

การตรวจพหูพจน์อัตโนมัติ

i18n-validate ตรวจว่าคีย์พหูพจน์ทุกคีย์กำหนดหมวดหมู่ CLDR ที่ภาษาเป้าหมายต้องใช้ หากไฟล์รัสเซียมีเพียง 'one' กับ 'other' เครื่องมือจะรายงาน 'few' และ 'many' ที่หายเป็นข้อผิดพลาด รันใน CI เพื่อจับก่อนผู้ใช้

Terminal
# Validate plural forms match CLDR requirements
npx i18n-validate --check-plurals \
  --source locales/en.json \
  --targets 'locales/*.json'

# Output:
# locales/ar.json:
#   items: missing plural forms: zero, two, few, many
#   Expected: zero, one, two, few, many, other
#   Found: one, other
#
# locales/pl.json:
#   items: missing plural forms: few, many
#   Expected: one, few, many, other
#   Found: one, other

# Fix: Add all required forms for each language
# See https://unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html
5

ICU MessageFormat สำหรับพหูพจน์

วิธีแนะนำคือ ICU MessageFormat ซึ่งใช้ข้อความเดียวพร้อมกฎฝัง : {count, plural, one {# item} other {# items}'} รองรับใน react-intl, vue-i18n, Angular Transloco, Flutter และเฟรมเวิร์กสมัยใหม่ส่วนใหญ่

ICU plural messages
// ICU MessageFormat handles plurals correctly
// It uses CLDR rules internally

// en.json
{
  "items": "{count, plural, one {# item} other {# items}}"
}

// ar.json
{
  "items": "{count, plural, zero {لا عناصر} one {عنصر واحد} two {عنصران} few {# عناصر} many {# عنصرًا} other {# عنصر}}"
}

// pl.json
{
  "items": "{count, plural, one {# element} few {# elementy} many {# elementów} other {# elementu}}"
}

// ICU also handles ordinals:
{
  "ranking": "{pos, selectordinal, one {#st} two {#nd} few {#rd} other {#th}}"
}
Terminal
# Validate ICU message syntax
npx i18n-validate --check-icu \
  --source locales/en.json \
  --targets 'locales/*.json'

# Catches:
# - Missing plural categories for the target language
# - Syntax errors in ICU messages
# - Mismatched variable names
# - Missing "other" (always required)
สัญลักษณ์ # ใน ICU MessageFormat จะถูกแทนด้วยจำนวนที่จัดรูปแบบ ใช้ # แทนการฮาร์ดโค้ดชื่อตัวแปร เช่น เขียน '# item / # items' แทนการทำซ้ำตัวแปร count

ลองใช้ i18n Agent ตอนนี้

ลากและวางไฟล์แปลของคุณที่นี่

JSON, YAML, PO, XML, CSV, Markdown, Properties

หรือคลิกเพื่อเลือกไฟล์

ภาษาเป้าหมาย

ไม่ต้องลงทะเบียนประเมินราคาได้ทันที

คำถามที่พบบ่อยเกี่ยวกับกฎพหูพจน์ CLDR