— SwiftUI, Interface Builder, Localization — 1 min read
Localization is a crucial aspect of modern app development, ensuring that your app can reach a global audience. In iOS development, both SwiftUI and Interface Builder provide robust tools for localizing your app. This article will guide you through the process of previewing localizations in SwiftUI - allowing you to see how your app's interface adapts to different languages and regions.
Before you can preview localizations, you need to add support for the desired languages and regions in your project. This involves:
.strings
files for different languages. You can also use the new Strings Catalogs instead now too - I wrote another article about this a little while back.SwiftUI provides a straightforward way to preview localizations by setting the locale environment variable.
Here's how you can set the locale to Japanese:
1struct ExampleView_Previews: PreviewProvider {2 static var previews: some View {3 ContentView()4 .environment(\.locale, .init(identifier: "ja"))5 }6}7
8// Or the alternative new syntax that uses macros:9
10#Preview {11 ContentView()12 .environment(\.locale, .init(identifier: "ja"))13}
In this example, ContentView
is previewed with the Japanese locale, allowing you to see how your interface will look to users that have set their device language to Japanese.
Interface Builder offers a different approach, allowing you to preview localizations using pseudolanguages before adding actual translations.