gpt4 book ai didi

swift - NSLocalizedString( key : value: comment: ) with variable is not working in swift

转载 作者:行者123 更新时间:2023-11-30 12:43:47 28 4
gpt4 key购买 nike

我需要本地化一个具有某些变量的字符串,并且本地化字符串必须使用key: value: comment 格式定义。各位,我尝试了很多方法,但还没有找到正确的方法。请参阅此处...

let timeValue = 5
let timeString = "hours"

//Bellows are working fine
dateLabelString = String(format: "Overdue by %d %@", timeValue,timeString) //Working //Normal flat string
dateLabelString = String(format: NSLocalizedString("Overdue by %d %@", comment: "Overdue by 5 days/hours/minutes/seconds"), timeValue,timeString) //Working
dateLabelString = String.localizedStringWithFormat(NSLocalizedString("Overdue by %d %@", comment: "Overdue by 5 days/hours/minutes/seconds"), timeValue,timeString) //Working

dateLabelString = NSString.init(format: "Overdue by %d %@", timeValue,timeString) as String //Working
dateLabelString = NSString.init(format: NSLocalizedString("Overdue by %d %@", comment: "Overdue by 5 days/hours/minutes/seconds"), timeValue,timeString) as String //Working


//Bellows are not working
dateLabelString = String(format: NSLocalizedString("OVERDUE_BY", value: "Overdue by %d %@", comment: "Overdue by 5 days/hours/minutes/seconds"), timeValue,timeString) //Not Working
dateLabelString = String.localizedStringWithFormat(NSLocalizedString("OVERDUE_BY", value: "Overdue by %d %@", comment: "Overdue by 5 days/hours/minutes/seconds"), timeValue,timeString) //Not Working
dateLabelString = NSString.init(format: NSLocalizedString("OVERDUE_BY", value: "Overdue by %d %@", comment: "Overdue by 5 days/hours/minutes/seconds"), timeValue,timeString) as String //Not Working

Xcode:8.2.1 Swift:2.3

Notice the problem is for including the key on NSLocalizedString, otherwise its working perfectly. I need the key must.

最佳答案

对于带有变量的本地化字符串,这对我有用:

String.localizedStringWithFormat(NSLocalizedString("MyCustomKey", comment: "Hello %d World"), myValue)

Localized.strings 中这一行:

MyCustomKey = "Hello %d World";

关于swift - NSLocalizedString( key : value: comment: ) with variable is not working in swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41905468/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com