Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS .stringsdict not working

I'm trying to use the .stringsdict functionality in iOS programming to display the proper ordinal suffix (st, nd, rd, etc...) for numbers. I've created the .stringsdict file for english, but it's only using the 'one' and 'other' keys. It's ignoring the 'two' and 'few' options. Anybody see what i've done wrong here?

The file looks like this:

<?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>TEAM_RANK_ORDINAL</key>
    <dict>
        <key>NSStringLocalizedFormatKey</key>
        <string>%#@ordinal_string@</string>
        <key>ordinal_string</key>
        <dict>
            <key>NSStringFormatSpecTypeKey</key>
            <string>NSStringPluralRuleType</string>
            <key>NSStringFormatValueTypeKey</key>
            <string>lu</string>
            <key>one</key>
            <string>%lust</string>
            <key>two</key>
            <string>%lund</string>
            <key>few</key>
            <string>%lurd</string>
            <key>other</key>
            <string>%luth</string>
        </dict>
    </dict>
</dict>
</plist>

I then access it like so, from Swift. For the value 1 it appends st, but every other number appends th:

    let fmt = NSLocalizedString("TEAM_RANK_ORDINAL", comment: "")
    for i in 0...30 {
        println(String(format: fmt, i))
    }
like image 852
Gargoyle Avatar asked May 09 '15 20:05

Gargoyle


2 Answers

The english locale ignores the "two" and "few" rules and uses only "one" and "other".

See Plural Rule Properties in the "Internationalization and Localization Guide":

The meaning of the categories is language-dependent, and not all languages have the same categories.

For example, English only uses the one, and other categories to represent plural forms. Arabic has different plural forms for the zero, one, two, few, many, and other categories. ...

There is (as far as I know) no way to use a .stringsdict file for producing ordinal numbers.

like image 73
Martin R Avatar answered Nov 12 '22 02:11

Martin R


While Martin answered your stringsdict question, for users who stumble across the "ordinal" issue, one should just use NumberFormatter for localized ordinal numbers. E.g.

let formatter = NumberFormatter()
formatter.numberStyle = .ordinal

for i in 1..<30 {
    print(formatter.string(for: i) ?? i)
}

Producing (for English users):

1st
2nd
3rd
4th
5th
6th
7th
8th
9th
10th
11th
12th
13th
14th
15th
16th
17th
18th
19th
20th
21st
22nd
23rd
24th
25th
26th
27th
28th
29th

And, FWIW, even if the stringsdict approach worked for two, etc., doesn't have a prayer of differentiating between large numbers that use a different ordinality syntax, e.g. between 122nd and 124th. You really need to use NumberFormatter for this.

like image 42
Rob Avatar answered Nov 12 '22 02:11

Rob