Description of Problem
We are exporting strings for localization using the command line tool xcodebuild -exportLocalizations.
However, Xcode does not respect the scheme or configuration that we specify. It always builds the Debug configuration.
This results in extraneous or incorrect strings being exported, e.g. strings from SwiftUI previews.
Note:
Exporting strings from within the Xcode IDE respects the selected scheme and configuration. However, exporting from the command line does not.
Our real app’s Xcode project cannot export strings from within the Xcode IDE at all, as Xcode always attempts to build our iOS project using the macOS SDK, which obviously fails. So we must use the command line export.
Also, a command line export is required for CI/CD pipelines.
Sample Code
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Text("Always")
#if DEBUG
Text("Debug-Explicit")
Text("Debug-Implicit")
#else
Text("Release-Explicit")
Text("Release-Implicit")
#endif
}
.padding()
}
}
// MARK: - Previews
#if DEBUG
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
VStack {
Text("Preview only string")
ContentView()
}
}
}
#endif
Localizable.strings
/* Appears in all builds */
"Always" = "Appears in all builds";
/* Appears in debug builds only */
"Debug-Explicit" = "Appears in strings file but only used in Debug builds";
/* Appears in release builds only */
"Release-Explicit" = "Appears in strings file but only used in Release builds";
Steps to Reproduce
Clone this project into a directory on your Mac, but do not open it in Xcode.
git clone https://dev.azure.com/slardieri/ReproRepos/_git/ExportRepro
Open a Terminal window and cd into that directory.
Run the following command to export strings for localization:
xcodebuild -exportLocalizations -sdk "iphoneos" -exportLanguage "en" -localizationPath "./ExportRepro Localizations" -project "./ExportRepro.xcodeproj" -scheme "ExportRepro Release" -configuration "Release"
Open the generated en.xliff file and observe which strings were exported.
open "./ExportRepro Localizations/en.xcloc/Localized Contents/en.xliff"
Expected Behavior
Based on the Release configuration specified in the command, you should see both the Release-Explicit and Release-Implicit strings, but not the Debug-Implicit or Preview only string.
Actual Behavior
What you actually get are the Debug-Explicit, Debug-Implicit, and Preview only strings, but not Release-Implicit.