Updated June 28, 2021

for developers

Plural support in Xcode (iOS/Mac) using Localizable.stringsdict

Bookmark Babble-on

You are clearly ready to localize your app. Why not let us help?

Try our free developer portal

Plural support in Mac and iOS using Localizable.stringsdict

As far back as 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 
		, @"Remove %ld selected file(s)" // Default (English) text
		, @"Confirmation dialog text" // comment for translator
		);
		

		let numfiles = 3
		Text( "Remove \(numfiles) selected file(s)", comment: "Confirmation dialog text")
		// All text in SwiftUI is automatically localizable. If you want to exclude text:
		Text( verbatim: "Sample text needs no translation" )
	
	
// In Xcode 13 (iOS 15)
	let numfiles = 3
	String(localized: "Remove \(numfiles) selected file(s)", comment: "Confirmation dialog text")
	
// earlier versions of Xcode and Swift
	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

This is a versatile, localizable string that we can pair with a Localizable.stringsdict file. What's that?

Stringsdict is a dictionary (plist) that stores all plural forms of the string. It exists separately from the Localizable.strings file, but will take precendence if you take the time to create it.

You only have to provide the English. Your translators will provide all the forms necessary for their language. Like magic, it *just works* at run time!

The Localizable.stringsdict file looks like the example plist here.

While that looks like a lot of work, it isn't. Xcode has a template Localizable.stringsdict file for you.
Just choose the template and write your zero, one, and many forms for English only.

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 singular, "1 file"

two, double, "2 files"

few, more than 2 but less than many, "3 files"

many, plural many, "5 files"

other, edge cases, "1.5 files"

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.


<plist version="1.0">
	<dict>
		<key>Remove %ld selected file(s)</key>
		<dict>
			<key>NSStringLocalizedFormatKey</key>
			<string>%#@files@</string>
			<key>files</key>
			<dict>
				<key>NSStringFormatSpecTypeKey</key>
				<string>NSStringPluralRuleType</string>
				<key>NSStringFormatValueTypeKey</key>
				<string>ld</string>
				<key>zero</key>
				<string>No files selected</string>
				<key>one</key>
				<string>Remove selected file</string>
				<key>other</key>
				<string>Remove %ld selected files</string>
			</dict>
		</dict>
	</dict>
</plist>

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.

How do you make sure plurals are correct in your iOS app?

Hire the best translators. All the extra work we do is at no extra charge — just pay for the words you want to translate.

  • Native translators
  • Obsessively detailed
  • Simple pricing
  • Lightening-fast updates for your help center
  • Help editing your English texts
  • QA assistance

Try us.

Contact