I have a bunch of custom views. Below is a minimal example:
struct PairView: View {
let left: String
let right: String
var body: some View {
HStack(alignment: .firstTextBaseline) {
Text(left)
.foregroundColor(.black)
Text(right)
.foregroundColor(.gray)
}
}
}
In application code I write sometimes hardcoded strings, so "1000" and "$" are expected to be written in Localizable.xcstrings automatically by Xcode. This is great!
PairView(left: "1000", right: "$")
However for Previews I have hardcoded strings too but they are dummy text. Not real stuff and I don't want them to be in the Localizable.xcstrings file.
#Preview {
PairView(left: "nacho", right: "4d")
}
How can I void strings in SwiftUI Previews being written to Localizable.xcstrings?
I read about verbatim (ex: Text(verbatim: "...") stuff but I could not find information on how apply it for custom views.
Use the
verbatimparameter in theTextinitializer. This prevents Xcode from treating these strings as needing localization. However, applying theverbatimmethod directly in custom views might not be straightforward sinceverbatimis an initializer parameter forTextviews, not a universal feature for all views.Instructions: