XCode 15 Localized strings with prefix operator function issue.

I have currently migrated to String catalogs. I have a prefix operator function like below to set a localized string and its working fine.

prefix operator &&

prefix func &&(key: String) -> String {

    NSLocalizedString(key, comment: "")

}

Usage:

 let sampleText = &&"alpha.beta.gamma"
 label.setText(sampleText)

But with XCode 15, strings from the code are not added automatically to the catalog file. But if I declare like below in the code, its working as expected.

let text = NSLocalizedString("Hello world", comment: "")
label.setText(sampleText)

Thought and insights will be much appreciated.

In order to be automatically extracted by Xcode, strings need to meet one of the following criteria:

  • A string literal passed directly to NSLocalizedString or one of the similar macros
  • A string literal passed directly to a custom function or macro that was added to LOCALIZED_STRING_MACRO_NAMES
  • A string literal that evaluates to a LocalizedStringKey, String.LocalizationValue, or LocalizedStringResource (with SWIFT_EMIT_LOC_STRINGS enabled)

So to get your example to work, you could do:

prefix func &&(string: LocalizedStringResource) -> String {
    String(localized: string)
}

let sampleText = &&"alpha.beta.gamma"

However, please note that comments can be very valuable for translators, and using this approach prevents comments from being included with strings. So using String(localized:comment:) directly can provide better localization.

Thank you for your response. Since our base deployment target is iOS 13 we can't adapt to LocalizedStringResource at the moment. I know that NSLocalizedString is of function type, but is there any way to pass it in the parameter?

XCode 15 Localized strings with prefix operator function issue.
 
 
Q