DevPrep

All topics › Accessibility and localization

Plurals and formatting: numbers, dates, Measurement

Middle PRO

Plurals

English has two forms (one, other), Russian four categories (one: 1, 21, 101; few: 2–4, 22–24; many: 5–20, 25–30; other: fractions), Arabic six. So count == 1 ? "file" : "files" is already wrong for Russian "2 файла" and "21 файл".

The right way is one string with a number and plural variations in the String Catalog:

// In the catalog, the key "%lld files" gets the forms:
// en: one "%lld file", other "%lld files"
let text = String(localized: "\(count) files")

The number's type in the interpolation matters: Int gives %lld, and the variations are bound to it.

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store