This subchapter is provided as a free sample for SwiftUI Fundamentals book.
You can read this subchapter online or download the sample bundle with PDF and EPUB by clicking the link below.
Download free sampleTo get access to the contents of the whole book you need to purchase a copy.
FREE SAMPLE - online version
Localization in SwiftUI
Localization ensures that user-facing text adapts to the user’s language, region, and culture. SwiftUI simplifies this process by automatically treating string literals in many built-in components, such as Text
, Label
, Button
, and others, as localizable. These components use LocalizedStringKey
for string literals, ensuring translations are applied at runtime based on the user’s locale without requiring additional configuration in most cases.
Button(
"Watch trailer", // LocalizedStringKey
action: playTrailer
)
# Xcode integration
When the project is built, Xcode automatically includes localizable strings from SwiftUI views in the string catalog, provided one is present in the project. The string catalog offers a centralized and efficient way to manage translations and organize all user-facing text.
SwiftUI supports adding comments to provide translators with additional context. These comments can be included when initializing Text
, ensuring translations are accurate and aligned with the intended usage.
Text(
"Showtimes",
comment: "Header for the list of movie showtimes"
)
If we need to provide a comment for the label of another SwiftUI component, such as a button, we can use the initializer that accepts a view instead of the convenience one that takes a string literal directly.
Button(action: displayShowtimes) {
Text(
"View showtimes",
comment: "Button to display showtimes"
)
}
When exporting the localization catalog, Xcode includes the comment alongside the string, ensuring that translators have all the necessary information.
For larger projects, translations can be organized by splitting them into multiple catalogs. We can reference specific catalogs using the tableName
parameter, which helps maintain scalability and clarity in projects with numerous localizable strings.
Text("Explore movies", tableName: "Navigation")
For advanced scenarios, such as strings defined in external frameworks or modules, we can specify additional parameters like bundle to control where the system looks up translations.
Text("Cast & Crew", bundle: Bundle(for: MovieDetails.self))
# String interpolation
SwiftUI allows us to include variables within text using string interpolation. This makes it easy to combine dynamic content, such as dates and numbers, with static text while ensuring the entire string is properly localized.
Text("Release date: \(movie.releaseDate, style: .date)")
When we embed variables into a Text
view, SwiftUI automatically converts them into format specifiers in the exported localizable strings files and catalog. The resulting key for the example above would be Release date: %@
.
At runtime, SwiftUI replaces the placeholder with the appropriately formatted value, such as in the French translation shown below, ensuring the string adapts to the user’s locale.


Localized string interpolation in SwiftUI supports a wide range of types, including numeric values, Foundation types, and even SwiftUI components like Text
and Image
.
Text("Enjoy \(Image(systemName: "popcorn")) at the movies!")
In this example, the Image
is embedded directly into the Text
view. At runtime, the image appears inline with the text, maintaining the visual and contextual consistency of the string.


# Explicitly localizing strings
For strings that are not directly tied to SwiftUI views, such as those used in custom models or general Swift structures, we can use the String(localized:)
initializer. This explicitly marks the string as localizable and ensures it is included in the localization catalog.
let movieTitle = String(
localized: "Spirited Away",
comment: "Title of the featured movie in the app"
)
This initializer also allows us to provide comments for translators, similar to how comments are added in SwiftUI Text
views.
By leveraging SwiftUI’s built-in localization features and seamless integration with Xcode’s string catalog, we can efficiently adapt our apps to different languages and regions, providing users with a tailored and intuitive experience.