0Pricing
Swift Academy · Lesson

Pluralization and Stringsdict

Handle plural rules across languages.

The Plurals Problem

English has two forms — 1 item, 2 items — so developers often write "\(n) item" + (n == 1 ? "" : "s"). This breaks badly: Arabic has six plural categories, Russian three, Japanese one. Hardcoding plural logic is a localization bug waiting to happen.

// FRAGILE — only works for English
let n = 5
let bad = "\(n) item" + (n == 1 ? "" : "s")
print(bad)

Plural Categories

Unicode defines plural categories: zero, one, two, few, many, other. Each language uses a subset. The system chooses the right category for a number based on locale rules — you never write the rules yourself.

// English uses: one, other
// Russian uses: one, few, many, other
// Japanese uses: other (only)
// You provide a variant string per category needed.

All lessons in this course

  1. String Catalogs and Localized Strings
  2. Pluralization and Stringsdict
  3. Locale-Aware Formatting
  4. Right-to-Left and Layout Direction
← Back to Swift Academy