SwiftUI: Variable strings not exported for localization

Solution 1:

Ok, so after tinkering with localization some more, I found an approach that works well.

The tutorial I linked to (https://www.ibabbleon.com/swiftui_localization_tutorial.html) does not talk about this, and makes things confusing with LocalizedStringKey and Text(String, comment: String). Some other tutorials also go down this route, but it makes code really ugly because it moves data into Text. So if you want to separate data from the view, you have to include Text (a UI element) in the view model. Alternatively, if you use LocalizedStringKey, you can't include comments for the translator.

Fortunately, there is a much simpler way!

struct ContentView: View {
  let normalString = "This is not exported for localization. Good for previews!"
  let localizedString = String(localized: "This is exported for localization.", comment: "You can add a comment for the translator, too!")

  var body: some View {
    VStack {
      Text(normalString)
      Text(localizedString)
    }
  }
}