skip to content

In SwiftUI, how does Text("\(nights) nights") become a String Catalog key, and how does the catalog supply Arabic's plural forms?

level: middleimportance: must knowfreq 62%

answer

  1. interpolation becomes a format specifier
  2. Int maps to %lld
  3. vary by plural in the catalog
  4. Arabic needs six categories

basics

~20 s

Interpolating an Int into a Text literal produces the key "%lld nights". The String Catalog stores plural variations for that key per language, and the number picks the category: one and other in English and German, six categories in Arabic.

solid answer

~40 s

The literal becomes a `LocalizedStringKey`, and each interpolation becomes a format specifier in the key: an `Int` becomes `%lld`, a `Double` `%lf`, a `String` `%@`. So the key is `%lld nights`, and Xcode extracts it into the String Catalog. There you mark the entry to vary by plural, and each language stores the categories its grammar needs: `one` and `other` for English and German; `zero`, `one`, `two`, `few`, `many` and `other` for Arabic. At runtime the number selects the category and fills the specifier. A `nights == 1 ? "night" : "nights"` branch hard-codes English grammar, creates two unrelated keys and is wrong in Arabic for most counts.

code

swift · 15 lines
swift
import SwiftUI

struct StaySummary: View {
    let nights: Int
    let guests: Int
    let nightsLabel: String

    var body: some View {
        VStack(alignment: .leading) {
            Text("\(nights) nights")                     // key "%lld nights"
            Text("\(nights) nights, \(guests) guests")   // key "%lld nights, %lld guests"
            Text("\(nightsLabel) nights")                // key "%@ nights": no plural rule can apply
        }
    }
}

go deeper

for a junior

Recall that an Int interpolated into a Text literal becomes %lld in the key and that plural forms live in the String Catalog, not in code.

for a middle

Explain the specifier mapping by type, how a plural variation stores one value per category, and why Arabic needs six categories.

for a senior

Spot the silent failures: String counts that give %@, concatenated Text fragments, count branches in code and missing English variations.

for a principal

Decide the team rule: numeric arguments for every counted noun, one sentence per key, and plural coverage checked in each language before release.

## From interpolation to catalog key A string literal passed to `Text` becomes a `LocalizedStringKey`, and its interpolations are not baked into the text. Each interpolated value is replaced in the **key** by a printf-style **format specifier**, and the value is kept as an argument. SwiftUI chooses the specifier from the value's type: | Interpolated type | Specifier in the key | Example key | |---|---|---| | `Int`, `Int64` | `%lld` | `%lld nights` | | `Int32`, `Int16`, `Int8` | `%d` | `%d nights` | | `Double`, `CGFloat` | `%lf` | `%lf km` | | `String`, `Text` | `%@` | `Your stay at %@` | So `Text("\(nights) nights")` with an `Int` looks up **`%lld nights`**. When you build, Xcode extracts that key from the code into the app's **String Catalog** (a `.xcstrings` file) with English as the source language, and translators fill in each language. ## Plural variations in the String Catalog A String Catalog entry can **vary by plural**. Instead of one translated value, a language stores one value per **plural category**, and at runtime the number passed for the specifier selects the category. The catalog is JSON; the German part of the entry above looks like this: ```json "%lld nights" : { "localizations" : { "de" : { "variations" : { "plural" : { "one" : { "stringUnit" : { "state" : "translated", "value" : "%lld Nacht" } }, "other" : { "stringUnit" : { "state" : "translated", "value" : "%lld Nächte" } } } } } } } ``` When the package or app builds, Xcode compiles that entry into the runtime plural format (a `.stringsdict` rule keyed on `lld`), so `Text` needs no extra code. An entry can also **vary by device**, for example a shorter string on Apple Watch than on iPhone. ## Why Arabic is the real test The categories come from Unicode's plural rules, and languages need different sets: - **English and German** use `one` and `other`: 1 night, 2 nights; 1 Nacht, 2 Nächte. - **Arabic** uses all six: `zero`, `one`, `two`, `few`, `many` and `other`. Arabic has a dual form for exactly two, a form for small counts and another for larger ones. - Other languages use other subsets, which is why the rule must live in data the translator controls rather than in code. A `nights == 1 ? "night" : "nights"` branch therefore works in English, roughly works in German, and cannot be right in Arabic, where zero, two, three to ten and eleven to ninety-nine each take a different form, so a single plural string is wrong for most counts. It also produces two separate keys, neither of which is a plural entry. ## Several numbers in one sentence A travel screen often needs "3 nights, 2 guests". `Text("\(nights) nights, \(guests) guests")` gives the key `%lld nights, %lld guests`. In the String Catalog each number in such an entry can carry its own plural variations, so the translator writes the agreement for nights and for guests independently and keeps the sentence order their language needs. ## Keys, comments and copy changes Because SwiftUI keys are usually the English source text, the key and the copy are the same string. That has two consequences a team should know: - **Editing the English copy creates a new key.** Changing `"\(nights) nights"` to `"\(nights) nights selected"` produces `%lld nights selected`; on the next build Xcode adds the new entry and marks the old one as stale, so every language needs translating again, including its plural variations. - **The comment is the translator's only context.** A plural entry shows translators a specifier and a set of categories, not your screen. A `comment:` such as "Number of nights in the booking summary" tells them what is being counted, which some languages need in order to choose the right noun form. ## Traps 1. **A `String` argument.** `Text("\(nightsLabel) nights")` with a `String` produces `%@ nights`. A plural rule needs a numeric argument, so no category can be chosen. 2. **Concatenated pieces.** `Text("\(nights)") + Text(" nights")` splits the sentence into fragments a translator cannot reorder or inflect. 3. **Grammar in code.** Ternaries or `switch` over the count encode English rules and bypass the catalog. 4. **Forgetting the source language.** English also needs its `one` variation; without it, "1 nights" ships. The point an interviewer listens for: the number travels as an argument, the plural rule lives in the catalog, and SwiftUI's interpolation produces the key that connects the two.

  • How do you localize "3 nights, 2 guests" when both numbers need agreement?
    Keep one sentence: `Text("\(nights) nights, \(guests) guests")` gives the key `%lld nights, %lld guests`. In the String Catalog each number in that entry can carry its own plural variations, so the translator writes agreement for nights and guests separately and orders the sentence for their language. Splitting it into two Texts loses that control.
  • Why does an English source string also need a plural variation?
    Without a `one` variation in English, the single value `%lld nights` is used for every count, so a one-night stay reads "1 nights". The source language is a language like any other: give the entry `one` and `other` there too.

  • A count ternary in code is enough for every language
  • Every language has just singular and plural forms
  • Interpolating a String count still triggers the plural rule
  • The key keeps the variable name, such as \(nights) nights
  • Plural variations require String(localized:) and do not work with Text