我正在尝试在 iOS 7 中使用对复数更复杂本地化的新支持。我创建了一个 .stringsdict 文件,根据 Foundation 发行说明(以及 Cocoa WWDC session 中的新功能)中的信息进行格式化。我已经验证 .stringsdict 正在被复制到我的应用程序包中(并且确实 -[NSBundle pathForResource:...]
找到了它)。但是,+[NSString localizedStringWithFormat:]
不会返回根据配置字典中的规则格式化的字符串。
代码:
- (IBAction)textFieldEditingDidEnd:(UITextField *)sender
{
NSInteger numPeople = [sender.text integerValue];
self.textView.text = [NSString localizedStringWithFormat:
NSLocalizedString(@"%d people are in the room", @"%d people are in the room"), (long)numPeople];
}
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 people are in the room</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 one is</string>
<key>one</key>
<string>A person is</string>
<key>two</key>
<string>Two people are</string>
<key>other</key>
<string>%d people are</string>
</dict>
</dict>
</dict>
</plist>
我还在此处上传了一个完整的、非常简单的示例项目: https://www.dropbox.com/s/mfze377g0r1iqde/PluralDemo.zip 。您在文本字段中输入一个数字,然后会使用
-localizedStringWithFormat:
的结果更新标签。有没有人让这个工作?我在设置时做错了吗?
最佳答案
stringsdict(复数)适用于 OS X Mavericks 和 iOS 7,并且基于 Unicode CLDR(通用语言环境数据存储库)。英语仅支持零、一和其他。某些语言支持“两个”,例如阿拉伯语、希伯来语、爱尔兰语等。
资料来源:
更新(2015 年 9 月 1 日):
正如我在下面的回复中提到的(从 2014 年 1 月 13 日开始),常规字符串文件仍然是必需的,即使它是空的。