skip to content

In a SwiftUI booking app, copy reaches Text through a component's String property and a view model, and those screens stay English in German; why, and how do you fix it?

level: seniorimportance: should knowfreq 45%

answer

  1. the type changed before Text saw it
  2. StringProtocol overload again
  3. carry the key, not the result
  4. LocalizedStringResource in model code
  5. packages need bundle: .module

basics

~20 s

Once a literal is stored as String, Text takes the non-localizing StringProtocol overload and Xcode never extracts it. Keep LocalizedStringKey in view parameters, LocalizedStringResource in model code, String(localized:) only where a String is required, and bundle: .module inside packages.

solid answer

~40 s

`Pill(title: "Free cancellation")` looks localized, but if `title` is `String`, the literal becomes plain text, `Text(title)` uses `init<S: StringProtocol>(_:)` and nothing is looked up or extracted. Type the parameter as `LocalizedStringKey` so the literal stays a key. In a view model, store a `LocalizedStringResource` (iOS 16): it keeps key, table and bundle, and `Text(_:)` accepts it directly. Use `String(localized:comment:)` only for APIs that need a `String`; it resolves immediately. Inside a Swift package, the default bundle is the app's main bundle, so pass `bundle: .module`. In the current SDK, interpolating a plain enum into a key draws a deprecation warning, because it would show an unlocalized debug description.

code

swift · 28 lines
swift
import SwiftUI

enum BookingStatus { case confirmed, pending }

struct Pill: View {
    let title: LocalizedStringKey           // was String: Text(title) skipped the catalog
    var body: some View { Text(title).padding(6) }
}

@Observable @MainActor
final class BookingViewModel {
    var status: BookingStatus = .confirmed
    // Keeps key, table and bundle; looked up when Text displays it.
    var headline: LocalizedStringResource = "Booking confirmed"
    // Looked up once, now, into a plain String.
    var receiptTitle = String(localized: "Receipt", comment: "Title of the emailed receipt")
}

struct BookingScreen: View {
    let model: BookingViewModel
    var body: some View {
        VStack {
            Pill(title: "Free cancellation")     // literal becomes a LocalizedStringKey
            Text(model.headline)                 // Text(_: LocalizedStringResource), iOS 16
            Text("Status: \(model.status)")      // warns: unlocalized debug description
        }
    }
}

go deeper

for a junior

Recall that storing copy in a String variable turns off Text's lookup, even if it started as a literal.

for a middle

Explain the three carriers, LocalizedStringKey, LocalizedStringResource and String(localized:), and when each one performs its lookup.

for a senior

Diagnose the leak from symptoms, fix components, view models and packages, and treat interpolation deprecation warnings as defects.

for a principal

Make the typing rule part of review and component design so new screens cannot reintroduce String-typed interface copy.

## The symptom and its cause German users see English on some screens while other screens are translated. The translated screens pass literals straight to `Text`. The English ones route the copy through something typed **`String`**: a reusable component with `let title: String`, or a view model property `var headline = "Booking confirmed"`. At the call site `Pill(title: "Free cancellation")` looks localized, but the literal is converted to `String`, `Text(title)` then picks the `init<S: StringProtocol>(_:)` overload, and that overload never looks anything up. Xcode's extraction does not offer the string to translators either, so the String Catalog has no entry and nothing fails loudly. The fix is to keep the **localizable type** all the way to `Text`. ## Why the compiler stays quiet `Text` declares both initializers on purpose. The `StringProtocol` initializer is marked as a disfavoured overload, so a bare literal resolves to the `LocalizedStringKey` initializer; a value whose static type is already `String` has only one match, and the code compiles without a warning. Nothing in the type system says "this `String` was meant for users". The same holds one level up: a component with a `String` parameter accepts the literal happily, because `String` is also expressible by a literal. The bug is therefore invisible to the compiler and to unit tests that run in English, and appears only when someone looks at the screen in another language. ## The three localizable carriers | Carrier | Where it fits | When the lookup happens | |---|---|---| | `LocalizedStringKey` | Parameters of SwiftUI views and components | When `Text` renders it | | `LocalizedStringResource` (iOS 16) | Model and view model code, values passed across layers | When something displays or resolves it | | `String(localized:)` (iOS 15) | APIs that demand a `String`: a share sheet's text, a notification body, a UIKit property | Immediately, into a plain `String` | - **`LocalizedStringKey`** is SwiftUI's own type. Typing a component's parameter as `LocalizedStringKey` lets callers pass literals, which become keys, and lets `Text(title)` use the localizing initializer. - **`LocalizedStringResource`** is Foundation's value type. It stores the `key`, a `defaultValue`, the `table`, the `locale` and a `bundle` description (`.main`, `.forClass(_:)` or `.atURL(_:)`). `Text` has an `init(_: LocalizedStringResource)` since iOS 16, so a view model can hold a resource and the view displays it with no extra code. It is also `Codable`. - **`String(localized:table:bundle:locale:comment:)`** looks the string up at the moment it runs and returns ordinary text. Use it where a `String` is unavoidable, and remember the result is frozen: it no longer carries the key. ## Bundles and tables Every lookup reads one **table** (a String Catalog or strings file, `Localizable` by default) in one **bundle**. `Text`'s `bundle:` parameter defaults to `nil`, which searches the main bundle, and `LocalizedStringResource`'s `bundle:` defaults to `.main`. That default is wrong inside a **Swift package** feature module: SwiftPM compiles the package's catalog into the package's own resource bundle and generates `Bundle.module` to reach it. Package code must therefore pass `bundle: .module`, as in `Text("Free cancellation", bundle: .module)`, or the lookup searches the app's main bundle, usually finds nothing, and the English key is shown. Apple's SwiftPM documentation says to always use `Bundle.module` to access a package's resources. A `tableName:` (or `table:`) argument selects another catalog in the same bundle, which teams use to split strings by feature. ## Interpolating your own types `Text("Status: \(model.status)")` with a plain enum compiles, but in the iOS 27 SDK the generic `appendInterpolation` it falls back to is marked **deprecated**, and the compiler warns that localized string interpolation "produces an unlocalized, debug description for this type of value". German users would see the untranslated case name `confirmed` inside an otherwise translated sentence. The fix is to interpolate a value the localized interpolation supports: give the enum a `LocalizedStringResource` per case and interpolate that (`LocalizedStringKey` accepts a resource since iOS 16), or interpolate a `Text`. Conforming the enum to `CustomLocalizedStringResourceConvertible` helps only when the sentence itself is built as a `LocalizedStringResource`, as in `Text(LocalizedStringResource("Status: \(status)"))`; interpolated straight into a `Text` literal, the conforming enum still draws the warning. ## A review checklist 1. Component parameters that end in `Text` are `LocalizedStringKey`, never `String`. 2. View models hold `LocalizedStringResource`, not strings resolved at init time. 3. `String(localized:)` appears only where an API requires `String`, with a `comment:`. 4. Package code passes `bundle: .module` on every lookup. 5. Deprecation warnings from localized interpolation are treated as bugs, not noise. Data, such as hotel names from the server, keeps travelling as `String`; the rule is about interface copy only.

  • How should a status enum show a localized label inside an interpolated Text?
    Give the enum a computed property that returns a localized value per case, for example a `LocalizedStringResource` such as `"Confirmed"`, and interpolate that: a `Text` literal's interpolation accepts a `LocalizedStringResource` since iOS 16. Interpolating the enum itself falls back to a deprecated overload that inserts its debug description, and the compiler warns about it.
  • Why can a String(localized:) value in a long-lived model be a problem?
    It is resolved once, when the line runs, and the result is a plain `String` with no key. If the model outlives a change in the language it should be shown in, or is passed to a layer that needs to resolve it differently, the text cannot be looked up again. A `LocalizedStringResource` keeps the key, table and bundle until display.

  • A literal argument stays localizable even when the parameter is String
  • Text(String) checks the catalog first and falls back to the text
  • LocalizedStringResource only works in App Intents, not in Text
  • Package code finds its own catalog without passing a bundle
  • Interpolating an enum into a Text literal localizes its case name