|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Localization - Pluralizations" |
| 4 | +subtitle: "The easiest way to support plurals" |
| 5 | +date: 2025-04-29 07:00:00 -0300 |
| 6 | +tags: [ios] |
| 7 | +thumbnail-img: "/resources/ep86-localization-handling-plurals/thumbnail.png" |
| 8 | +readtime: true |
| 9 | +--- |
| 10 | + |
| 11 | +The String Catalogs file have built-in support for pluralization. |
| 12 | + |
| 13 | +[Official Apple Docs](https://developer.apple.com/documentation/xcode/localizing-and-varying-text-with-a-string-catalog) |
| 14 | + |
| 15 | +#### Table of Contents |
| 16 | +- [String Catalogs](#string-catalogs) |
| 17 | +- [Handling Plurals](#handling-plurals) |
| 18 | + - [Usage](#usage) |
| 19 | +- [Related Articles](#related-articles) |
| 20 | + |
| 21 | +# String Catalogs |
| 22 | + |
| 23 | +Adding a String Catalog to your app is as simple as creating the file: |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +For a detailed guide on how to add the `localizable` extension to `String`, read [this Localization article](/2022-12-27-new-app-localization/). |
| 28 | + |
| 29 | +# Handling Plurals |
| 30 | + |
| 31 | +Handling plurals has never been easier, the String Catalog has built-in support: |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +Forget about the `if/else` statements for good 🙌 |
| 38 | + |
| 39 | +## Usage |
| 40 | + |
| 41 | +Using it is as straight forward as using the in-line components: |
| 42 | + |
| 43 | +```swift |
| 44 | +@State private var itemCount = 0 |
| 45 | +Text("\(itemCount) Books") // 0 Books, 1 Book, 2 Books |
| 46 | +``` |
| 47 | + |
| 48 | +Or, if you are using the String extensions from the [previous localization article](/2022-12-27-new-app-localization/): |
| 49 | + |
| 50 | +```swift |
| 51 | +@State private var itemCount = 0 |
| 52 | +Text("books".localized(with: [itemCount])) // 0 Books, 1 Book, 2 Books |
| 53 | +``` |
| 54 | + |
| 55 | +--- |
| 56 | + |
| 57 | +# Related Articles |
| 58 | + |
| 59 | +- [New App - Localization](/2022-12-27-new-app-localization/) |
| 60 | +- [AdaptableStack](/2023-10-07-adaptable-stack/) |
| 61 | +- [Components](/2023-01-04-new-app-components/) |
| 62 | +- [Improve Build Times](/2023-08-18-improve-build-times-in-spm-packages-and-in-your-apps/) |
| 63 | +- [Xcode Templates](/2023-01-27-new-app-xcode-templates/) |
| 64 | +- [String Extensions](/2025-03-03-useful-string-extensions/) |
| 65 | +- [ViewStateController](/2023-03-04-view-state-controller/) |
| 66 | +- [ViewModifiers](/2023-01-03-new-app-view-modifiers/) |
| 67 | + |
| 68 | +--- |
| 69 | + |
| 70 | +<!-- Do not remove - SEO meta tags --> |
| 71 | +{% seo %} |
0 commit comments