Logo for tanaschita.com

How to localize plurals with Localizable.stringsdict files in iOS

Learn to support plurals for multiple languages.

updated on 06 May 2024 · 6 min read

In this article, we'll look at how to localize plurals with Localizable.stringsdict files. If you are using String Catalogs, check out this article instead.

If you are already familiar with localizing plurals in iOS and you are just looking to remember the specifics, you can directly scroll down to the cheat sheet at the end of the article.

Let's jump in.

Sponsorship logo
Preparing for a technical iOS job interview - updated for iOS 18
Check out my book on preparing for a technical iOS job interview with over 200 questions & answers. Test your knowledge on iOS topics such as Swift, SwiftUI, Combine, HTTP Networking, Authentication, SwiftData & Core Data, Concurrency with async/await, Security, Automated Testing, Machine Learning and more.
LEARN MORE

Every time we need to localize texts like My dog ate 2 carrots where the carrot count is dynamic, one localization string will not be enough. For example, localizing with My dog ate %i carrots would produce My dog ate 1 carrots when passing in 1.

The first solution that may come up could be to create another localization string for one carrot and adding some logic such as:

if carrotCount == 1 {
return NSLocalizedString("dog_eating_carrots_one")
} else {
return String(format: NSLocalizedString("dog_eating_carrots_multiple"), carrotCount)
}

The problem with the solution above is that different languages vary in how they handle plurals. For example, in Russian, we would need to handle more cases. Plural rules for the word dog look like this:

It would be complicated to handle this logic in code. Instead, we can use a Localizable.strigsdict file to solve it.

Unlike a Localizable.strings file, a Localizable.strigsdict file provides additional features to work with different languages, for example to define plural rules. The Localizable.strigsdict is able to interpret the arguments you pass in and select the right localized string based on it.

Creating and localizing a Localizable.stringdict file

To create a Localizable.stringdict file, we can select a .stringdict template from Xcode's dialog when creating a new file. The localization process is exactly the same as for Localizable.strings files.

Adding plural configuration

Now, we can add the plural configuration for a specific language. We can use the template as a starting point:

The .stringdict template
The .stringdict template

The configuration for our example could look as follows:

A .stringdict configuration example
A .stringdict configuration example

Let's go through it step by step.

  1. Localized String Key — is replaced with the key that we will use when calling NSLocalizedString. In our example the key is dog_eating_carrots.

  2. NSStringLocalizedFormatKey - the localized text with parameters. In our case it's My dog ate %#@carrotsCount@.. It has the following structure:

    • every variable is be preceded by %#@ and followed by @
    • we only need to define variables for parameters where plural rules should be applied
    • in case we need to pass in additional arguments, we can simply use the string format specifiers we already know such as %@, %d etc. For example, to pass in the dog's name, our format string would look as follows: My dog %@ ate %#@carrotsCount@.
  3. Variable — the rules dictionary that applies to a specific variable. We create one for every variable we defined in NSStringLocalizedFormatKey. In our example, it's one variable with the name carrotsCount.

  4. NSStringFormatSpecTypeKey — parameter processing rule. It's already set to NSStringPluralRuleType so we can leave it as is.

  5. NSStringFormatValueTypeKey — specifies the format of the parameter, for example d for integer or f for double.

  6. CLDR Language Plural Rules — we can choose different plural rules like zero, one, two, few, many and other. For some languages like English we only need to configure two rules, one and other. Other languages have only a single plural rule and some languages have more than two.

Switching between property list and source code

Xcode provides a user interface to work with property list files. Depending on your preference, you can directly work with source code instead. To switch, right-click on the Localizable.stringdict file and choose Open as -> Source Code. Switching back works the same way with Open as -> Property List.

The source code version of our example above looks as follows:

<?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>dog_eating_carrots</key>
<dict>
<key>NSStringLocalizedFormatKey</key>
<string>My dog ate %#@carrotsCount@.</string>
<key>carrotsCount</key>
<dict>
<key>NSStringFormatSpecTypeKey</key>
<string>NSStringPluralRuleType</string>
<key>NSStringFormatValueTypeKey</key>
<string>d</string>
<key>zero</key>
<string>no carrots</string>
<key>one</key>
<string>1 carrot</string>
<key>other</key>
<string>%d carrots</string>
</dict>
</dict>
</dict>
</plist>

Cheat sheet

And that's basically it. Below is the promised cheat sheet which shows another example with two variables.

Cheatsheet for localizing plurals in iOS
Cheatsheet for localizing plurals in iOS
Sponsorship logo
Preparing for a technical iOS job interview - updated for iOS 18
Check out my book on preparing for a technical iOS job interview with over 200 questions & answers. Test your knowledge on iOS topics such as Swift, SwiftUI, Combine, HTTP Networking, Authentication, SwiftData & Core Data, Concurrency with async/await, Security, Automated Testing, Machine Learning and more.
LEARN MORE

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

localization

swift

ios

How to use String Catalogs for pluralization in Swift

Learn about pluralization and variables in String Catalogs.

10 Jul 2023 · 3 min read

Latest articles and tips

© 2024 tanaschita.com

Privacy policy

Impressum