
A stringsdict file (a file with a .stringsdict file extension) is a property list used to define both language plural variants and presentation width variants. The stringsdict file contains a dictionary where the keys are the localizable strings and the values are dictionaries containing either plural variants or width variants.
A plural variant is a different translation of a formatted string that depends on the plural rules of the language. The keys are the formatted strings (%d file(s) remaining), and the values are dictionaries containing the variants, as in:
<plist version="1.0"> <dict> <key>%d file(s) remaining</key> <dict> … </dict> <key>%d service hour(s)</key> <dict> … </dict> <key>%d award(s)</key> <dict> … </dict> </dict></plist>In your code, you pass the same formatted strings containing variables—for example, @”%d file(s) remaining”, @”%d service hour(s)” and @”%d award(s)”—to the NSLocalizedString macro.
The keys for the formatted string dictionary are:
NSStringLocalizedFormatKey: A format string that contains variables.
A variable is preceded by the %#@ characters and followed by the @ character. For example, files is a variable in this format string:
<key>NSStringLocalizedFormatKey</key><string>%#@files@</string>[plural variant]: A dictionary that specifies the plural variant for a variable in the NSStringLocalizedFormatKey format string.
There’s a separate plural variant dictionary for each variable that appears in the NSStringLocalizedFormatKey format string. The plural variant dictionary determines which format string is returned by the NSLocalizedString macro. It contains a different format string for each grammatical plural variant in the language.
The keys for the plural variant dictionary are:
NSStringFormatSpecTypeKey: Specifies the type of language rule.
The only possible value is NSStringPluralRuleType, which indicates a language plural variant.
NSStringFormatValueTypeKey: A string format specifier for a number, as in the letter d for an integer.
zero, one, two, few, many, other: The format string for the language-specific plural category. For the categories and plural rules for each language, go to CLDR Language Plural Rules. The other key is required.
For example, the plural variant dictionary for the files variable in English is:
<key>files</key><dict> <key>NSStringFormatSpecTypeKey</key> <string>NSStringPluralRuleType</string> <key>NSStringFormatValueTypeKey</key> <string>d</string> <key>one</key> <string>%d file remaining</string> <key>other</key> <string>%d files remaining</string></dict>If the number is 1, the localized string becomes @”1 file remaining”. If the number is 2, the localized string becomes @”2 files remaining”.
The meaning of the plural variant 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. Although Russian also uses the many category, the rules for which numbers are in the many category are not the same as the Arabic rules.
All of the categories are optional except the other category. However, the localized text may be grammatically incorrect if you don’t supply a rule for all the language-specific categories. Conversely, if you provide a rule for a category not used by a language, it is ignored and the other format string is used.
Using the NSStringFormatValueTypeKey format specifier in the zero, one, two, few, many, and other format strings is optional. For example, for English, the one format string can be:
One file remainingwhile the other format string can be:
%d files remainingUse the format specifier or spell out numbers in the format strings. If you use a numeric in the format string, as in 1 file remaining for English, it may not be localized when the user changes the region (for example, if the number set changes). Instead, use the format specifier, as in:
%d file remainingOtherwise, spell out the number, as in:
One file remainingImportant: Avoid using a stringsdict file for a string that does not include a number, because a language might define generic singular and plural forms in ways that don’t work well with the one and other categories.
A width variant provides a version of a localized string for a specified presentation width in the user interface. Standard UIKit controls automatically get the appropriate string width variant for the current screen size, from the localized stringsdict file. In your code, you can also get a width variant using the variantFittingPresentationWidth method in the NSString class.
The width variant dictionary contains a single dictionary, where the key is NSStringVariableWidthRuleType, and the value is a dictionary with key-value pairs for each variant, as in:
<key>GDP</key> <dict> <key>NSStringVariableWidthRuleType</key> <dict> <key>1</key> <string>GDP (billions)</string> <key>20</key> <string>GDP (in billions)</string> <key>50</key> <string>Gross Domestic Product (in billions)</string> </dict> </dict>For details on specifying width variants, go to variantFittingPresentationWidth.