内容简介:翻译自:https://stackoverflow.com/questions/25862960/ios-swift-and-localizedstringwithformat
我在 swift
中找不到localizedStringWithFormat的替代品.我想要做的是使用Localizable.stringsdict,英语示例使用单数/复数本地化:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>%d record trovati</key>
<dict>
<key>NSStringLocalizedFormatKey</key>
<string>%#@num_people_in_room@ in the room</string>
<key>num_people_in_room</key>
<dict>
<key>NSStringFormatSpecTypeKey</key>
<string>NSStringPluralRuleType</string>
<key>NSStringFormatValueTypeKey</key>
<string>d</string>
<key>zero</key>
<string>No record</string>
<key>one</key>
<string>Only one record</string>
<key>other</key>
<string>Some records</string>
</dict>
</dict>
</dict>
</plist>
和代码:
[NSString localizedStringWithFormat:NSLocalizedString(@"%d record trovati", nil), totRecsFound];
有关如何在Swift中执行此操作的任何想法?
Swift中提供了相同的方法:
for totRecsFound in 0 ... 3 {
let str = NSString.localizedStringWithFormat(NSLocalizedString("%d record trovati", comment: ""), totRecsFound)
println(str)
}
输出:
No record in the room Only one record in the room Some records in the room Some records in the room
请注意,除了“Localizable.stringsdict”文件之外,还需要有一个
“Localizable.strings”文件(可能为空).
Swift 3/4更新:
for totRecsFound in 0 ... 3 {
let str = String.localizedStringWithFormat(NSLocalizedString("%d record trovati", comment: ""), totRecsFound)
print(str)
}
翻译自:https://stackoverflow.com/questions/25862960/ios-swift-and-localizedstringwithformat
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Designing for Emotion
Aarron Walter / Happy Cog / 2011-10-18 / USD 18.00
Make your users fall in love with your site via the precepts packed into this brief, charming book by MailChimp user experience design lead Aarron Walter. From classic psychology to case studies, high......一起来看看 《Designing for Emotion》 这本书的介绍吧!