
CLDR 复数规则:跨语言验证 i18n 复数类别
英语有 2 种复数形式,阿拉伯语有 6 种,俄语有 4 种。如果 i18n 设置只处理 'one' 和 'other',应用在大多数语言中都会出错。本指南说明 CLDR 复数规则及其验证方法。
什么是 CLDR 复数规则?
Unicode CLDR(Common Locale Data Repository)定义六个复数类别:zero、one、two、few、many 和 other。每种语言使用其中一部分,并有特定数字规则。英语使用 one(恰好为 1)和 other(其余)。但大多数语言更复杂,错误的复数形式会导致语法不通,使应用显得不专业。
// 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.六个复数类别
CLDR 只定义六个复数类别。并非每种语言都使用全部类别;英语只使用两个,阿拉伯语使用全部六个。i18n 文件必须定义每种目标语言所需的类别,否则用户会遇到崩溃、原始键或语法错误的文本。
// 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)各语言的复数类别
常见目标语言所需复数类别的快速参考。定义翻译文件中的复数形式时可使用此表。
// 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自动复数验证
i18n-validate 会检查翻译文件中的每个复数键是否定义目标语言所需的全部 CLDR 类别。如果俄语文件只有 'one' 和 'other' 形式,该工具会将缺失的 'few' 和 'many' 类别标为错误。在 CI 中运行它,可在复数问题影响用户前发现问题。
# 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用于复数的 ICU MessageFormat
定义复数翻译的推荐方式是 ICU MessageFormat。它使用嵌入复数规则的单个字符串:{count, plural, one {# 个项目} other {# 个项目}'}。react-intl、vue-i18n、Angular Transloco、Flutter 和大多数现代 i18n 框架都支持 ICU。
// 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}}"
}# 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)立即试用 i18n Agent
将翻译文件拖放到此处
JSON, YAML, PO, XML, CSV, Markdown, Properties
或点击选择文件
目标语言