Ios NSLocalizedString在Swift中带有变量

Ios NSLocalizedString在Swift中带有变量,ios,swift,nslocalizedstring,Ios,Swift,Nslocalizedstring,如何转换包含变量的字符串,如下所示: let alert = UIAlertController(title: NSLocalizedString("NEUEARTIKEL",comment:"Bitte gib einen neuen Artikel für \(titelArr[sender.tag]) an:"), message: nil, preferredStyle: .Alert) let math = "Math" let science = "Science" String.

如何转换包含变量的字符串,如下所示:

let alert = UIAlertController(title: NSLocalizedString("NEUEARTIKEL",comment:"Bitte gib einen neuen Artikel für \(titelArr[sender.tag]) an:"), message: nil, preferredStyle: .Alert)
let math = "Math"
let science = "Science"
String.localizedStringWithFormat(NSLocalizedString("I love %@ and %@", comment: "loved Subjects"), math, science)
当我在localizable.String中正常地转换字符串时:

NEUEARTIKEL="Add an item to \(titelArr[sender.tag]):";
警报将显示(titelar[sender.tag]),而不是其值

这可能很简单,但我是斯威夫特的新手,无法用谷歌搜索一些有用的东西!;-)

谢谢你的帮助
//Seb

在localisable中,您不能直接设置自定义文本,只能使用文本和格式标志。因此,为了实现你的目标,你可以这样做:

NEUEARTIKEL="Add an item to %@:";
之后,使用
NSString(格式:,…)


完成后,您的可本地化字符串将被格式化为您想要的格式。

这是另一种方式,我是这样做的

let NEUEARTIKEL = "Add an item to %@:"
let alert = UIAlertController(title: String.localizedStringWithFormat(NSLocalizedString(NEUEARTIKEL, comment: "Bitte gib einen neuen Artikel für \(titelArr[sender.tag]) an:"), titelArr[sender.tag]), message: nil, preferredStyle: .Alert)
基本上,格式本地化字符串的主要思想如下:

let alert = UIAlertController(title: NSLocalizedString("NEUEARTIKEL",comment:"Bitte gib einen neuen Artikel für \(titelArr[sender.tag]) an:"), message: nil, preferredStyle: .Alert)
let math = "Math"
let science = "Science"
String.localizedStringWithFormat(NSLocalizedString("I love %@ and %@", comment: "loved Subjects"), math, science)
您可以找到gist

的可能副本