Skip to Content
Course content

209: Pluralization Rules with Localization

Click on the "Edit" button in the top corner of the screen to edit your slide content.

You've probably already handled basic translation by putting strings in a Localizable.strings file. That works great for "Settings" or "Logout," but it falls apart the moment you have to deal with counts. In English, we only have two forms: singular and plural. But if you're targeting a language like Arabic or Polish, you might have six different plural forms depending on the number. If you try to handle that in your Swift code, you'll end up with a nightmare of if/else blocks that make your view controllers unreadable.

The temptation of the ternary operator

When I first started building apps, I used to take the "quick and dirty" approach. Let's say we're building a notification center and we want to show how many unread messages a user has. I'd likely write something like this:

let count = 5
let message = count == 1 ? "You have \(count) unread notification" : "You have \(count) unread notifications"
notificationLabel.text = NSLocalizedString(message, comment: "")

It looks clean for English, right? But I quickly realized I made a huge mistake here. By putting the logic in the Swift code, I've hardcoded the English pluralization rule. If a translator wants to localize this for a language where "zero" is treated differently than "many," they can't. They're stuck with my ternary logic. We need to move the "decision" of which string to use out of the code and into the localization system itself.

Moving the logic into a stringsdict file

To do this properly, we use a .stringsdict file. Think of this as a specialized dictionary that tells iOS: "Based on this number, pick the correct version of this sentence."

First, I'll create a file named Localizable.stringsdict. In Xcode, this is essentially an XML file, but the editor gives us a nice table to work with. I'll define a key called "unread_notifications_count"`.

Inside that key, I'll set the "Plural Kind" to Right-to-Left (if applicable) or simply Offset, but usually, we use Plural. Then I can define the specific rules:

  • One: "You have %d unread notification"
  • Other: "You have %d unread notifications"

I'll also add a Zero case here, because "You have 0 unread notifications" feels a bit robotic. I'd rather it say "You're all caught up!"

Wiring it up in the view

Now that the logic lives in the .stringsdict file, the Swift code becomes incredibly simple. I no longer care if the count is 1, 0, or 1,000. I just pass the number to the localization function, and the system handles the rest.

let count = 0 
// The system looks at the .stringsdict file and picks the 'Zero' string automatically
let formatString = NSLocalizedString("unread_notifications_count", comment: "Notification count message")
notificationLabel.text = String.localizedStringWithFormat(formatString, count)

By using String.localizedStringWithFormat, we're telling Swift to look for a variable substitution (the %d we put in the dictionary) and use the value of count to determine which plural rule to apply. It's a much more professional way to handle it, and it keeps your UI logic separated from your linguistic logic.




📋 Practical Task

Implement Pluralization for a Shopping Cart

You are building a shopping cart summary screen. Currently, the app uses a simple string that looks awkward when there is only one item (e.g., "1 items in your cart").

Your Task:

  1. Create a hypothetical Localizable.stringsdict entry for the key "cart_item_count".
  2. Define three rules for this key:
    • Zero: "Your cart is empty"
    • One: "You have %d item in your cart"
    • Other: "You have %d items in your cart"
  3. Write the Swift code required to fetch this localized string and inject a variable named itemCount into it using String.localizedStringWithFormat.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.