4

I'm trying to use the new support for more sophisticated localization of plurals in iOS 7. I've created a .stringsdict file, formatted according to the information in the Foundation release notes (and What's New In Cocoa WWDC session). I've verified that the .stringsdict is being copied into my app's bundle (and indeed -[NSBundle pathForResource:...] finds it). However, +[NSString localizedStringWithFormat:] doesn't return a string formatted according to the rules in the configuration dictionary.

Code:

- (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>

I've also uploaded a complete, very simple sample project here: https://www.dropbox.com/s/mfze377g0r1iqde/PluralDemo.zip . You enter a number in the text field, and a label is updated with the result of -localizedStringWithFormat:.

Has anyone gotten this to work? Have I done something wrong in setting it up?

4

2 回答 2

7

这里的问题是您缺少Localizable.strings文件。

即使它是空的,它也是必需的

用你的项目尝试过,添加文件使项目工作得很好。

于 2014-06-25T08:42:49.510 回答
5

stringsdict(复数)适用于 OS X Mavericks 和 iOS 7,并且基于 Unicode CLDR(通用区域设置数据存储库)。英语仅支持零、一和其他。某些语言支持“二”,例如阿拉伯语、希伯来语、爱尔兰语等。

资料来源:

更新(2015 年 9 月 1 日):

正如我在下面的回复中提到的(从 2014 年 1 月 13 日起),仍然需要常规字符串文件,即使它是空的。

您是否还添加了一个空字符串文件?文档说即使字符串文件为空,您也必须添加字符串文件,这可能是由于 Cocoa 中本地化的内部实现。

于 2014-01-13T15:55:27.507 回答