带有变量的NSLocalizedString(key:value:comment :)在swift中不起作用

时间:2017-01-28 01:20:14

标签: swift nslocalizedstring formatted

我需要本地化一个包含一些变量的字符串,并且必须使用 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

  

注意问题是在NSLocalizedString上包含密钥,否则它的工作完美。我需要钥匙。

1 个答案:

答案 0 :(得分:0)

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

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

Localizable.strings这一行:

MyCustomKey = "Hello %d World";