DEV Community

Monica Granbois
Monica Granbois

Posted on • Originally published at monicagranbois.com

2

SwiftUI: Previewing localized text in the canvas

Last week I watched the Swift packages: Resources and localization video from WWDC20. One thing I learned was that the Canvas can preview localized values. This is done by adding a locale to a View via the environment.

Example:

import SwiftUI

struct ContentView: View {
    var body: some View {
       Text("greeting")
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView().environment(\.locale, Locale.init(identifier: "fr"))
    }
}

Enter fullscreen mode Exit fullscreen mode

The above code assumes that a French Localizable.strings file exist with a greeting key. For example:

"greeting" = "Bonjour le monde!";
Enter fullscreen mode Exit fullscreen mode

The EnvironmentValues documentation lists all the values that can be set.

Gif showing the Canvas preview switching greeting text between english, french and german.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay