Automatic plurals with inflect: true

Text("^[\(count) item](inflect: true)") picks "1 item" or "2 items" for you, with no if statements and no plural rules of your own.

iOS 15+macOS 12+ Swift 5.9

count == 1 ? "item" : "items" works in English and breaks the moment the app is translated. Foundation’s automatic grammar agreement handles it for you: mark the words that depend on a number, and the text is inflected to match.

BasketView.swift
import SwiftUI

struct BasketView: View {
    @State private var count = 1

    var body: some View {
        Form {
            Stepper(value: $count, in: 0...20) {
                // "0 items", "1 item", "2 items"
                Text("^[\(count) item](inflect: true) in your basket")
            }

            // Adjectives agree too: "1 new message", "3 new messages"
            Text("You have ^[\(count) new message](inflect: true)")
        }
    }
}

#Preview {
    BasketView()
}

Outside SwiftUI, the same markup works with AttributedString:

let count = 3
let label = AttributedString(localized: "^[\(count) photo](inflect: true) selected")
print(String(label.characters))   // "3 photos selected"

How it works

Gotchas

See also