
Regras de plural CLDR: validar categorias de plural de i18n entre idiomas
O inglês tem 2 formas de plural. O árabe tem 6. O russo tem 4. Se sua configuração de i18n só tratar de 'one' e 'other', sua aplicação está danificada na maioria dos idiomas. Este guia explica as regras de plural CLDR e como validá-las.
O que são as regras de plural CLDR?
O Unicode CLDR (Common Locale Data Repository) define seis categorias de plural: zero, one, two, few, many e other. Cada idioma utiliza um subconjunto destas categorias com regras numéricas específicas. O inglês utiliza one (exatamente 1) e other (tudo o resto). Contudo, a maioria dos idiomas é mais complexa e formas plurais erradas causam texto gramaticalmente incorreto, o que dá à sua aplicação um aspecto amador.
// 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.As seis categorias de plural
CLDR define exatamente seis categorias de plural. Nem todos os idiomas utilizam as seis: o inglês só utiliza duas e o árabe utiliza todas. Seus arquivos de i18n devem definir as categorias exigidas por cada idioma de destino; caso contrário, os usuários veem falhas, chaves sem tratamento ou texto gramaticalmente incorreto.
// 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)Categorias de plural por idioma
Referência rápida das categorias de plural exigidas por idiomas de destino comuns. Consulte-a ao definir formas plurais nos seus arquivos de tradução.
// 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 translatedValidação automatizada de plurais
i18n-validate verifica se cada chave de plural nos seus arquivos de tradução define todas as categorias CLDR exigidas pelo idioma de destino. Se seu arquivo russo só tiver as formas 'one' e 'other', a ferramenta assinala as categorias 'few' e 'many' em falta como erros. Execute-a em CI para detectar problemas de plural antes de chegarem aos usuários.
# 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 para plurais
A forma recomendada de definir traduções plurais é ICU MessageFormat. Utiliza uma única string com regras de plural incorporadas: {count, plural, one {# item} other {# items}'}. ICU é compatível com react-intl, vue-i18n, Angular Transloco, Flutter e a maioria dos frameworks de i18n modernos.
// 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)Experimente já o i18n Agent
Solte aqui seu arquivo de tradução
JSON, YAML, PO, XML, CSV, Markdown, Properties
ou clique para selecionar
Idiomas de destino