Khoury
Khoury

Reputation: 431

How to add plural in Stringsdict file format without showing the number value in the string shown

Using a stringsdict file in Xcode, how would I go about supporting plurals for labels that will not show a number in the final text shown?

For example, if I had a label "Days Since" and another label with the corresponding number, how would enter this into the stringsdict file when they are two separate labels?

Thanks.

Upvotes: 0

Views: 944

Answers (1)

Fizker
Fizker

Reputation: 2634

This is as simple as you would hope. Add the number to the localizable string, but don't use it in the translation. It is totally OK to not mention the variable in a translation; maybe you want to type zero or one instead of 0 or 1.


For example, in SwiftUI, the following will look for a plural translation: Text("\(count) days").

The normal english stringsdict file for this would look like the following, and result in 0 days, 1 day, 2 days, etc:

<?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>%lld days</key>
    <dict>
        <key>NSStringLocalizedFormatKey</key>
        <string>%#@days@</string>
        <key>days</key>
        <dict>
            <key>NSStringFormatSpecTypeKey</key>
            <string>NSStringPluralRuleType</string>
            <key>NSStringFormatValueTypeKey</key>
            <string>d</string>
            <key>one</key>
            <string>%d day</string>
            <key>other</key>
            <string>%d days</string>
        </dict>
    </dict>
</dict>
</plist>

To achieve your goal, simply remove the %d from the translations. The results will then be days (for 0), day (for 1), days (for 2+):

<?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>%lld days</key>
    <dict>
        <key>NSStringLocalizedFormatKey</key>
        <string>%#@days@</string>
        <key>days</key>
        <dict>
            <key>NSStringFormatSpecTypeKey</key>
            <string>NSStringPluralRuleType</string>
            <key>NSStringFormatValueTypeKey</key>
            <string>d</string>
            <key>one</key>
            <string>day</string>
            <key>other</key>
            <string>days</string>
        </dict>
    </dict>
</dict>
</plist>

Upvotes: 3

Related Questions