Plural support in Mac and iOS using Localizable.stringsdict
Starting in OS X 10.9 and iOS 7, Apple added plural support for localized strings. Rejoice! That means you no longer need to code:
String.localizedStringWithFormat( NSLocalizedString("remove.files.dialog", value: "Remove %ld selected file", comment: "Confirmation dialog text"), count); if (count > 1) {// logic to add an "s" for plurals ...
This approach barely works for English and is a huge mess in other languages. Did you know most languages have very different rules for making words plural than English? Take a look at the official Unicode list of plural rules here. Hopefully one glance is enough to convince you that using Apple's built-in solution is best for your sanity and your users'.
Create just %i string(s) instead
The long-term solution to plurals on iOS and Mac is to create just one string:
NSLocalizedStringWithDefaultValue(@"remove.files.dialog" // Unique key of your choice , @"Localizable", // .strings file name , [NSBundle mainBundle] // Default bundle , @"Remove %ld selected file(s)" // Default (English) text , @"Confirmation dialog text" // comment for translator );
NSLocalizedString("remove.files.dialog", // Unique key of your choice value:"Remove %ld selected file(s)", // Default (English) text comment:"Confirmation dialog text") // comment for translator
While that looks like a lot of work, it isn't. We've built a Localizable.stringsdict generator for you below!
Just choose your strings and type in the singular and plural forms.
Stringsdict is the only way to do plurals correctly for multiple languages, which have different plurals when the number ends in 2-4, or multiples of 10, fractions, or other crazy things you never expected. These have been systematically divided into named categories: one
, two
, few
, many
, other
, and so on. Don't worry though: your translators will know the difference between few
and many
if it applies to their language. Some languages, including many Asian languages, have no plurals at all.
Note a couple of cool things from the Remove %ld selected files example above that works in English too. We can easily add precise languages for cases like zero
: No files selected or remove the number completely for one
: Remove selected file.
Start by importing your .strings
, en.xliff
or .stringsdict
file here.
Add blank string
Or create a .stringsdict file from scratch.