Xcode "Export Localizations..." fail to extract array of String.LocalizationValue

Any and all LocalizedStringKey and String.LocalizationValue initialization string literals should be extracted with the Xcode "Export Localizations..." command. But an array of String.LocalizationValue is not extracted:

    // 👎👎👎 but an array of String.LocaizationValue are not extracted!!! 👎
    let charcuteries = [
        String.LocalizationValue("Prosciutto"),
        "Capicola",
    ]

works for LocalizadStringKey:

    // works!! 👍👍👍
    let wines = [
        LocalizedStringKey("Barbera"),
        "Zinfandel"
    ]

I think the extract tool is based on compiler generated output and should handle all String.LocaizationValue initialization. FB9787096

Edit: so to workaround this problem, I am making a copy and change the type from String.LocalizationValue to LocalizedStringKey so the "Export Localizations..." tool can extract the strings:

enum WorkaroundCopyHereForLocalizationExtract {
    static let junk =  Array<LocalizedStringKey>([
        "Prosciutto",
        "Capicola",
    ])
}

I keep the original array of String.LocalizationValue for my real code.

@luca_bernardi

My test
import SwiftUI

struct Foo: View {
    let text: LocalizedStringKey  // text will be extracted

    var body: some View {
        Text(text)
    }
}

struct Bar: Identifiable, View {
    let id = UUID()
    let text: String

    init(localized text: String.LocalizationValue) {
        self.text = String(localized: text)
    }

    var body: some View {
        Text("Localized ->") + Text(text.first).font(.largeTitle).foregroundColor(.mint) + Text(text.dropFirst().dropLast()) + Text(text.last).font(.largeTitle).foregroundColor(.red)
    }
}

extension Text {
    init(_ c: Character?) {
        self.init(c == nil ? "" : String(c!))
    }
}

struct ContentView: View {
    // these are extracted 👍
    let coffees: [Bar] = [
        .init(localized: "Greater Alarm"),
        .init(localized: "Ambrosia"),
    ]

    // String.LocalizationValue by itself is extracted 👍
    let name1 = String.LocalizationValue("Paul")
    // String.LocalizationValue initialize in this form is extracted 👍
    let name2: String.LocalizationValue = "Jimmy"
    let name3: LocalizedStringKey = "Patty"

    #warning("these are not extracted")
    // 👎👎👎 but an array of String.LocaizationValue are not extracted!!! 👎
    let charcuteries = Array<String.LocalizationValue>([
        "Prosciutto",
        "Capicola",
    ]).map(Bar.init(localized:))

    // works!! 👍👍👍
    let wines = Array<LocalizedStringKey>([
        "Barbera",
        "Zinfandel"
    ])

    // for interpolation
    let gibberish = "abba+cadabra"
    let n = 123
    let d = 123.0
    var body: some View {
        VStack {
            Text("Hello, world!")
                .padding()
            Foo(text: "My name is \("Paul")")   // extracted 👍
            Bar(localized: "Aaa")               // extracted 👍
            Bar(localized: name1)
            Bar(localized: "Blahblah \(gibberish) \(n) \(d) haha")       // extracted 👍
            ForEach(coffees) {
                $0
            }
            ForEach(charcuteries) {
                $0
            }
        }
    }
}
Terms of Service

Privacy Policy

Cookie Policy