Logo for tanaschita.com

How to use String Catalogs for pluralization in Swift

Learn about pluralization and variables in String Catalogs.

10 Jul 2023 · 3 min read

At WWDC23, Apple introduced String Catalogs which replaces the way we add localization to iOS projects. In the previous article on how String Catalogs work, we covered the basics on localization with String Catalogs.

In this article, we'll look at how we can use them for pluralization.

Sponsorship logo
Preparing for a technical iOS job interview - updated for iOS 17
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

Before String Catalogs, we had to use .stringdict files to add localized pluralization support. To learn how that worked, check out this article on how to localize plurals by using the Localizable.stringsdict file.

String Catalogs come with built-in support for String pluralization. To localize plurals with String Catalogs, we have to:

  1. Define a format specifier within the localization key, for example applesSelection.title.%lld
  2. Use the format specifier within the translations, for example %lld apples
  3. Right-click on the created key-value and choose Vary by plural:
Vary by plural.
Vary by plural option in Xcode
  1. Xcode prefills all values for us. What's left to do is to adjust the translations for different pluralization cases:
Localized plurals in a String Catalog
Localized plurals in a String Catalog.

Now, we can use the localized String as follows:

LocalizedStringKey("applesSelection.title.\(5)")

In case we need pluralization for more than one value in a String, we can use variables:

String Catalog variables.
String Catalog variables

Xcode provides options to create variables when we add more than one format specifier. We can also define them by enclosing the variable name as follows: %#@applesCount@

Sponsorship logo
Preparing for a technical iOS job interview - updated for iOS 17
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 localization in Swift

Learn how to work String Catalogs introduced at WWDC23.

26 Jun 2023 · 4 min read

Latest articles and tips

© 2024 tanaschita.com

Privacy policy

Impressum