I have a bunch of strings in my several SwiftUI previews, but the problem is that they get included in Xcodes Export Localizations feature. Is there a way to automatically prevent that strings from PreviewProvider’s get exported for localization?
I’ve already tried to wrap the PreviewProvider structs in #if DEBUG, but that that didn’t work.
Text(verbatim: "")String("")Though, that’s really annoying to wrap all of the previews strings this way. I can’t be the only one that wants to exclude the preview strings, right?
There is no official way to exclude a string from the String Catalog.
However, if you embed your string into another string, it's enough for the Catalog to not recognize it anymore.
So you can just add this extension
extension String {
var excludeLocalization: String { String(self) }
}
If the string already existed in the Catalog, you will probably need to manually remove it. If you can't figure out how to manual remove it, use excludeLocalization > temporary change the string (so if you had "lorem ipsum", change it to "lorem" for example) > build the project (it should remove it from the catalog) > put back your string "lorem ipsum" and keep the excludeLocalization
As requested, here is a complete example
#Preview {
List {
Section {
SomeView()
} footer: {
Text("SomeView footer".excludeLocalize)
}
Section {
ForEach(0...10, id: \.self) { iter in
Text("Number \(iter)".excludeLocalize)
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With