Ilya
Ilya

Reputation: 332

SwiftUI preview canvas ignores locale setting

This doesn't work:

.environment(\.locale, .init(identifier: "es"))

The preview canvas still shows the english values... Localization works properly if I run the app on the emulator and change the language in the device settings. But it does not work in the preview canvas.

Here is the code I'm using:

Text("SomeLocalizableStringKey")
...
static var previews: some View {
        ForEach(["iPhone 8", "iPhone X", "iPhone XS Max"], id: \.self) { deviceName in
            MainContentView()
                .environment(\.locale, .init(identifier: "es")) // << this seems to be ignored by the preview
                .previewDevice(PreviewDevice(rawValue: deviceName))
                .previewDisplayName(deviceName)
                
            }
    } 

XCode 12.5.1

Upvotes: 18

Views: 2532

Answers (1)

Sagar Joshi
Sagar Joshi

Reputation: 1024

This is how I got it working.

  1. Edit Scheme -> Run -> Options -> Set the language you want under App language.

  2. Clean Build Folder (Command + Shift + K)

Upvotes: 9

Related Questions