SwiftUI dynamic localized strings
Xcode allows for translations that integrate into SwiftUI components, with dynamic data, described in [LOCALIZATION].
NOTE: In Xcode 15, Apple introduced the “String catalog” format, which is the recommended way to handle translations, described in [STRING-CATALOG]. The principles in this post still apply, but targets primarily the “Strings file (Legacy)” format.
Data in localized strings
// Localizable.strings (sv) "Your fruit is %@" = "Din frukt är %@"; "You have %lld fruits" = "Du har %lld frukter";
You can provide these strings directly into most components, and have the text be translated.
// … in code Text("Your fruit is \(fruitName)") Text("You have \(fruitCount) fruits")
The variable needs to be interpolated with
Required interpolation and type
If you provide the wrong type for the substitution, or do not interpolate at all, no translation will be made.
Text("You have \("123") fruits") // wrong: incorrect type Text("Your fruit is Orange") // wrong: no interpolation
- Apple Inc., Localization, Apple Developer Documentation
- Apple Inc., Localizing and varying text with a string catalog, Apple Developer Documentation