Skip to Content
Course content

208: String Catalogs for Localization

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

I'll be honest: for years, localization in iOS was a chore. We had to maintain these clunky .strings files, manually typing keys that had to match our code exactly. One typo, and your app just displayed the raw key—like "login_button_title"—to the user. Then there were .stringsdict files for plurals, which were basically XML nightmares that no one wanted to touch.

The headache of manual keys

Let's look at a scenario I'm dealing with right now. I'm building a fitness tracker, and I have a simple view that tells the user how many calories they've burned. Initially, I just wrote it like this:

Text("You burned \(calories) calories today!")

It works in English, but I need this to support Spanish and French. In the old workflow, I'd have to create a Localizable.strings file, manually add "You burned %lld calories today!" = "¡Quemaste %lld calorías hoy!";, and hope I didn't mess up the format specifier. It's tedious and error-prone.

Letting Xcode do the heavy lifting

I want to see if the new String Catalogs can handle this for me. First, I'll go to File > New > File and search for "String Catalog." I'll name it Localizable.xcstrings. At first glance, the file is completely empty. I'm thinking, "Wait, do I still have to type everything in here?"

Actually, no. Here is where the magic happens. I'm going to build my project (Cmd+B). When I check the Localizable.xcstrings file again, the string "You burned \(calories) calories today!" has automatically appeared in the list. Xcode scanned my SwiftUI views, found the Text literals, and pulled them into the catalog for me.

Now, I can just click the "+" button to add Spanish, and I get a nice grid where I can type the translation right next to the original English string. No more jumping between a text editor and a spreadsheet.

Taming the plural nightmare

But here is the real test: plurals. If the user burns exactly 1 calorie, saying "1 calories" sounds robotic and wrong. In the past, I would have had to dive into a .stringsdict file to define "one" and "other" categories. Let's see how the catalog handles this.

I'll right-click the translation key in the String Catalog editor and select "Vary by Plural." Suddenly, Xcode gives me a nested structure. I can now provide a specific translation for the "One" case and a different one for "Other."

  • One: "You burned 1 calorie today!"
  • Other: "You burned %lld calories today!"

I don't have to write any logic in my Swift code like if calories == 1 { ... } else { ... }. The system looks at the number I passed in and automatically picks the right string from the catalog based on the user's locale rules. This is a massive win for maintainability.

Tracking the gaps

One thing I've always struggled with is knowing exactly what percentage of the app is actually translated. I used to have to search the whole project for untranslated strings. In the String Catalog, I noticed there's a little percentage bar next to each language. As I fill in the Spanish translations, the bar fills up. If I add a new feature with new strings, the bar drops, telling me exactly where I need to go back and add translations. It's a simple visual cue, but it saves me from shipping an app that's half-English and half-Spanish.




📋 Practical Task

Implementing a Localized Workout Summary

You are tasked with adding localization to a "Workout Summary" screen. Instead of hardcoding strings or using old-school strings files, you will implement a String Catalog to handle dynamic content.

Requirements:

  • Create a new .xcstrings file named Localizable.xcstrings in your project.
  • In your SwiftUI view, implement a Text view that displays the number of sets completed using a string like: "Completed \(setCount) sets!".
  • Build the project to ensure the string is automatically extracted into your String Catalog.
  • Add "French" as a target language in the catalog.
  • Implement Pluralization for the "sets" string in both English and French (handling the difference between 1 set and multiple sets).
  • Verify that the translation percentage for French reaches 100% for this specific string.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.