Test Your Localized SwiftUI App
Written by Team Kodeco
Testing your localized application is crucial to ensure that the translations and regional formats are correctly applied. This chapter will guide you through the process of running your localized SwiftUI app in various languages and utilizing Xcode’s previews for quick visual inspection of localized content.
Running Your App in Different Languages
To run your app in a different language or region, you can adjust the settings of your run scheme in Xcode.
Here are the steps:
- In Xcode, go to the top menu and choose Product ▸ Scheme ▸ Edit Scheme.
- In the left column of the dialog that appears, select Run, then click the Options tab on the right.
- Under Options, scroll down to choose the language from the App Language dropdown menu, and select a region from the App Region dropdown menu. The System Region uses the operating system’s default region settings. [Development Region] represents the region you’re developing the app in, and other options represent specific regions.
- Click Close and then click the Run button in the toolbar.
Here’s what Xcode should look like:
Your app will now run using the specified language and region settings.
Using Previews for Localization Debugging
SwiftUI’s previews can be a powerful tool for quickly testing localizations. You can set the locale of a preview by adding the environment(\.locale
modifier and passing the desired language code identifier with Locale(identifier:)
.
For example:
struct ContentView: View {
@State private var number: Double = 1234.56
var body: some View {
VStack {
Text("Your total is")
Text("\(number, specifier: "%.2f")")
.font(.title)
.foregroundColor(.green)
}
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
Group {
ContentView()
.environment(\.locale, Locale(identifier: "en"))
.previewDisplayName("English")
ContentView()
.environment(\.locale, Locale(identifier: "fr"))
.previewDisplayName("French")
ContentView()
.environment(\.locale, Locale(identifier: "ar"))
.previewDisplayName("Arabic")
}
}
}
Your preview should look like this:
This will render previews of the ContentView in English, French and Arabic, enabling you to visually inspect the localized content without running the entire app.
Automation and Continuous Integration
While manual testing and using SwiftUI previews are excellent for localized app testing, consider leveraging continuous integration (CI) for systematic and regular testing of your localizations, especially if you’re supporting multiple languages and regions. In the bot configuration in Xcode, you can specify the language and region settings, allowing the bot to run tests in the specific localization environment.
Remember, localizing your app is only the first step in creating an internationally friendly application. Testing your localized application thoroughly ensures that it delivers a great user experience to your global audience.