iOS .stringsdict not working
Asked Answered
B

2

7

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))
    }
Betook answered 9/5, 2015 at 20:28 Comment(0)
G
11

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.

Gemina answered 9/5, 2015 at 20:34 Comment(4)
Yah, I just found that. Makes the stringsdict file a big complicated mess for very little gain. Sigh. I hope they extend it to be more useful going forward. I just submitted an enhancement request.Betook
The categories are not arbitrary; they are defined by Unicode. See unicode.org/cldr/charts/latest/supplemental/…. The primary purpose of stringsdict is to make grammatically correct pluralization possible in languages other than English. As a side benefit it removes the need to have an if/else check for having a separate singular string.Dabchick
@Betook Any luck with your enhancement request? :)Cinerary
you save my day!Communalism
I
5

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.

Included answered 25/12, 2018 at 17:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.