
Правила множини 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.htmlICU MessageFormat для множини
Рекомендований спосіб визначення перекладів із формами множини — ICU MessageFormat. Він використовує один рядок із вбудованими правилами множини: {count, plural, one {# item} other {# items}'}. ICU підтримують react-intl, vue-i18n, Angular Transloco, Flutter і більшість сучасних фреймворків i18n.
// 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
або натисніть, щоб вибрати
Цільові мови