dahpgjgamgan
dahpgjgamgan

Reputation: 3007

xcodebuild --exportLocalization produces invalid content when used with SwiftUI Text(NSLocalizedString("key", value:"value")

I'm running into an issue when exporting strings to an xliff file with xcode (14.2)

my code looks like this:

Text(NSLocalizedString("[feature] string key", value: "string value", comment: "string comment"))

When I export localizations (either though xcode ui or with xcodebuild --exportLocalizations I get a warning like this:

WARNING: Key "[feature] string key" used with multiple values. Value "[feature] string key" kept. Value "string value" ignored

and indeed the xliff file contains the equivalent content:

<trans-unit ... id="[feature] string key" ...>
  <source>[feature] string key</source>
  <target>[feature] string key</target>
  <note>string comment</note>
</trans-unit>

The issue goes away when the code is adjusted to store NSLocalizedString in a variable, sorta like this:

let x = NSLocalizedString("[feature] string key", value: "string value", comment: "string comment")
Text(x)

Having that workaround is ok (and other workarounds might also exist), but it's pretty easy to mistakenly use the invalid pattern, and I'd like to understand why this would be happening in the first place and if there's a way to fix it. The project has been using Text(NSLocalizedString(...) to some extent, it never differentiated between a string keys and their english values (which is what apple recommends afaik)

I have little iOS/apple ecosystem developer experience and my only hypothesis is that the offending code is interpreted as something like this during extraction:

NSLocalizedString("[feature] string key", value: "string value", comment: "string comment")
Text("[feature] string key")

The latter being then "folded" into a string which key and value are both equal to "[feature] string key", but don't know a way to see through the opaqueness of the localization export process.

Some guides (namely: how-to-localize-swiftui and swiftui localization tutorial recommend the exact pattern that's not working for me.

Out of notable build settings the project I'm encountering this is using SWIFT_EMIT_LOC_STRINGS is set to NO, whille LOCALIZED_STRING_SWIFTUI_SUPPORT is set to YES.

Upvotes: 0

Views: 142

Answers (1)

Andrew Meier
Andrew Meier

Reputation: 41

I'm running into this as well. I noticed that if I pull my NSLocalizedString into a variable it works as expected.

var body: some View {
    Text(x)
}

var x: String {
    NSLocalizedString("some.random.key", value: "An English String", comment: "A test string!")
}

Upvotes: 0

Related Questions