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

蓝咒 提交于 2019-12-25 08:07:05

问题


I need to localize a string which has some variable and the localized string must be defined with key: value: comment format. Guys I tried many ways but don't get the right way yet. See here...

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.


回答1:


For localized strings with variables this is working for me:

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

And in the Localizable.strings this line:

MyCustomKey = "Hello %d World";


来源:https://stackoverflow.com/questions/41905468/nslocalizedstringkey-value-comment-with-variable-is-not-working-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!