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.
Hello and welcome to Apple Developer Forums,
I will comment on a few things here.
-
Preview-only strings should no longer be extracted as of Xcode 16, regardless of the RELEASE/DEBUG configuration.
-
Regarding this:
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.
Assuming this is in the context of Swift Packages, this issue should be fixed in Xcode 16 Beta 4. If you are still experiencing issues with Xcode exporting for an unexpected platform, please file a Feedback report with a sample project.
-
A localization export (whether in the IDE or on the command line) is expected to cover an entire project or workspace and is not scheme-based. Thus, Xcode does not support exporting within the context of a specific scheme.
-
Export does not support the
-configuration
option on the command line today, but please file a Feedback report for us to add that functionality.