Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude Strings in Previews from Localization Export

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.

My Current Workaround

  1. Use Text(verbatim: "")
  2. Wrap other strings in 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?

like image 305
alexkaessner Avatar asked Aug 07 '26 05:08

alexkaessner


1 Answers

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)
            }
        }
    }
}
like image 78
Kalzem Avatar answered Aug 09 '26 18:08

Kalzem