SwiftUI -本地化注解在.xcloc文件中不可见

yuvru6vn  于 2022-12-21  发布在  Swift
关注(0)|答案(1)|浏览(206)

我尝试添加本地化注解,但在编辑器中打开时,它没有显示在导出的本地化文件(. xcloc文件)中。
我如何向一个值数组添加本地化注解,以便它在导出的文件中实际可见?
下面是我的代码:

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")
j9per5c4

j9per5c41#

我试过你的代码,测试过不同的场景,但我不知道为什么会发生这种情况,我唯一能说的是,不知何故,这与将本地化存储在变量中有关。
如果你直接将值存储为文本视图,它就能工作。我希望这对你有帮助。

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
    }
}

相关问题