本文介绍了带有变量的NSLocalizedString(key:value:comment:)不能快速工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对具有某些变量的字符串进行本地化,并且必须使用 key:value:comment 格式定义本地化的字符串.伙计们,我尝试了很多方法,但是还没有找到正确的方法.看到这里...

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

推荐答案

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

For localized strings with variables this is working for me:

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

Localizable.strings这一行中:

MyCustomKey = "Hello %d World";

这篇关于带有变量的NSLocalizedString(key:value:comment:)不能快速工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 14:59