NSLocalizedString在Swift中不起作用

时间:2018-05-02 04:47:38

标签: swift localization

界面本地化字符串存在这个问题:

var friendsNumber = 3
var firstString = NSLocalizedString("I have 3 friends.", comment: "") // the translation works good

var secondString = NSLocalizedString("I have \(friendsNumber) friends.", comment: "") // the translation doesn’t work

我该怎么做?

1 个答案:

答案 0 :(得分:0)

第二种情况需要的是格式字符串。

var messageFormat = NSLocalizedString("I have %d friends.", comment: "")
var message = String(format: messageFormat, friendsNumber)

然后在字符串文件中,您最终得到:

英文:

"I have %d friends" = "I have %d friends";

西班牙语:

"I have %d friends" = "Tengo %d amigos";