Reputation: 45118
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.
Upvotes: 0
Views: 289
Reputation: 914
Use the verbatim
parameter in the Text
initializer. This prevents Xcode from treating these strings as needing localization. However, applying the verbatim
method directly in custom views might not be straightforward since verbatim
is an initializer parameter for Text
views, not a universal feature for all views.
struct PreviewText: View {
var text: String
var body: some View {
Text(verbatim: text)
}
}
Instructions:
#Preview {
PairView(left: PreviewText(text: "nacho").text, right: PreviewText(text: "4d").text)
}
Upvotes: 1