Reputation: 21373
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?
Upvotes: 4
Views: 3180
Reputation: 2864
stringsdict (plurals) works in both OS X Mavericks and iOS 7 and is based on Unicode CLDR (Common Locale Data Repository). English supports only zero, one, and other. "Two" is supported in some languages, such as Arabic, Hebrew, Irish, etc.
As I mentioned in a response below (from Jan 13, 2014), the regular strings file is still required, even if it's empty.
did you also add an empty strings file? The documentation says that you must add the strings file even if it's going to be empty, probably due to the internal implementation of localization in Cocoa.
Upvotes: 5
Reputation: 71
The problem here is that you are missing Localizable.strings
file.
It is required even if it is empty.
Tried it with your project and adding the file made the project work just fine.
Upvotes: 7