Text("Hello \(user)") How Do i localice Hello?

Asked on 06/14/2025

1 search

To localize the text "Hello (user)" in SwiftUI, you can use the localization features provided by Xcode. Here's a general approach based on the session "Code-along: Explore localization with Xcode" from WWDC:

  1. Add a String Catalog: Start by adding a string catalog to your project. This can be done through the File menu in Xcode. The default name is "Localizable", and it should be placed in the Resources group.

  2. Localize Strings: Most SwiftUI APIs, including Text, are localizable by default. You can use String.localized to make strings available for localization. For example, you can define your string in the string catalog and use it in your SwiftUI code.

  3. Use Interpolation: For strings with interpolated variables, such as "Hello (user)", you can define a placeholder in your string catalog. Xcode will generate a function for strings with placeholders, using the placeholder name as the argument label.

  4. Generate Symbols: Xcode can generate symbols for manually added strings, which can be used anywhere a localized string resource is needed, including SwiftUI views like Text or Button.

For more detailed steps, you can refer to the session Code-along: Explore localization with Xcode (00:01:00) where these concepts are demonstrated.