user14119170
user14119170

Reputation: 1891

SwiftUI - Localization comment not visible in .xcloc file

I am trying to add the localization comment, but it does not show up in the exported localisations file (.xcloc file) when opened in the editor.

How can I add a localization comment to an array of values, so that it is actually visible in the exported file?

Here's my code:

let fontWeightNames: [LocalizedStringKey] = ["Ultra Light", "Thin", "Light", "Regular", "Medium", "Semibold", "Bold", "Heavy", "Black"]

var currentFontWeightName: LocalizedStringKey {
     fontWeightNames[fontWeights.firstIndex(of: currentFontWeight)!]
}

Text(currentFontWeightName, comment: "Inside Font Weight Picker")

Upvotes: 0

Views: 71

Answers (1)

Alex Luque
Alex Luque

Reputation: 172

I've tried your code and tested different scenarios but I can't tell why is this happening. The only thing I can say is that somehow it is related to the fact of having the localizations stored in variables.

If you store the values directly as Text views, it works. I hope this could be helpful to you.

struct ContentView: View {
    private let fontWeightNames: [Text] = [
        Text("Ultra Light", comment: "Ultra Light comment"),
        Text("Thin", comment: "Thin comment"),
        Text("Light", comment: "Light comment"),
        Text("Regular", comment: "Regular"),
        Text("Medium", comment: "Medium comment"),
        Text("Semibold", comment: "Semibold comment"),
        Text("Bold", comment: "Bold comment"),
        Text("Heavy", comment: "Heavy"),
        Text("Black", comment: "Black comment")
    ]
    private var fontWeights: [Text] {
        [fontWeightNames[2], fontWeightNames[1]]
    }
    private var currentFontWeight: Text {
        fontWeightNames[1]
    }
    private var currentFontWeightName: Text {
         fontWeightNames[fontWeights.firstIndex(of: currentFontWeight)!]
    }
    
    var body: some View {
        currentFontWeightName
    }
}

enter image description here

Upvotes: 1

Related Questions