Ramaraj T
Ramaraj T

Reputation: 5230

iOS - Hanling plural noun localization with .stringsdict - not working

I am trying to implement plural rules localisation. i.e., I expect "One file..." when the argument is 1, "2 files..." when the argument is 2 and likewise.

I followed the Apple guidelines. But I always get the results for plural, even for 1 i.e, "1 files...".

I've created a Localizable.stringsdict file with the following content (exactly like the apple guidelines).

<?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 messages in your inbox</key>
        <dict>
            <key>NSStringLocalizedFormatKey</key>
            <string>%#@messages@ in your inbox</string>
            <key>messages</key>
            <dict>
                <key>NSStringFormatSpecTypeKey</key>
                <string>NSStringPluralRuleType</string>
                <key>NSStringFormatValueTypeKey</key>
                <string>d</string>
                <key>one</key>
                <string>One message is</string>
                <key>other</key>
                <string>%d messages are</string>
            </dict>
        </dict>
    </dict>
</plist>

This is how I use this key in code

    NSLog(@"%@", [NSString localizedStringWithFormat:NSLocalizedString(@"%d messages in your inbox", @"Message shown for remaining files"), 1]);

What could be the problem?. Here is the sample code I have tried so far.

Upvotes: 1

Views: 1844

Answers (2)

Ting Yi Shih
Ting Yi Shih

Reputation: 876

It is simply the filename Localizable.stringsdict that was followed by a whitespace behind... Remove the whitespace in the filename and everything will work fine. No need to recreate the file.

According to the initial commit that the author gave, you can see that the filename LocalizationTest/en.lproj/Localizable.stringsdict is followed by a whitespace... that's why it is broken. Rename it and everything will go fine.

Upvotes: 0

Vladimir Kofman
Vladimir Kofman

Reputation: 1993

While debugging, I've noticed that Localization.stringsdict file is just ignored. When I've recreated it, everything started to work properly. I guess the original file was created with the wrong type: the correct type should be Property List.

I've sent a pull request as you suggested.

Upvotes: 1

Related Questions