Every year, Apple tries to improve the localization process to make it more comfortable. Every new Xcode release gradually extends our toolset in that matter. In this article, I want to present a couple of the latest features introduced up from the Xcode 9.
Let's say you need to add a booking page in your app, on which you present the number of people in a format like: „Reservation for 1 person”, „Reservation for 5 people” (note the “people” form) etc.
If you try to do it using switch-cases and Localizable.strings keys, you will find a problem when adding new languages. In English, we have 2 plural rules for nouns (like in the example), but in Polish there are 3, and Arabic have it 6 (you can read more about it here). And those rules can be mutually exclusive!
Handling that manually could be really complicated and messy. Thankfully, Apple introduced String Dictionaries. It’s a .plist file that allows us to parametrize our content using predefined rules.
To do that we need to put a special tag (it has to start with %#@ and ends with @ character), and add a list of supported cases for the rule we are using.
For now, Xcode supports only pluralization, but the set of rules will be extended in the future.
You can read more about it on in the Apple documentation.
let format = NSLocalizedString("booking.people.counter", comment: "")
The listing above presents the way how we can load our content. First, we need to get the format string, which we are doing the same way as with Localizable.strings files. Then, we need to apply our rule (2nd line). We can add more than variables to a single key.
Import / Export
Up from the Xcode 10, you can export everything that you marked as localizable (strings, string dictionaries, images, HTML pages, etc.) into the Xcode Localizable Catalog bundle. All strings will be aggregated into the XLIFF file, which is an industry standard for translations sharing and is commonly supported by many tools.
After updating the images, translations and everything, it can be easily imported back to our project without any additional action from our side.
If you ever thought about the meaning of comment parameter required when reading the content using NSLocalizedString macro here is the answer: During the export, Xcode analyses the source code as well and collects all of those comments. Then those are applied as an additional field in XLIFF file, which can give additional context for the translator. Because a “book” word can be a noun or a verb, and it makes a huge difference.
What’s new in Xcode 11
The new Xcode version will give us a new rule in String Dictionaries. From now, we will be able to distinguish our content depending on which device we are running the app. So when it is a macOS app, we can present a text like “Click here to continue”, when on iPad it would be “Tap here to continue”.
Next feature is more spectacular. Up to now, after exporting the content, we had to apply all notes by ourselves. But with the update, Xcode can add the screenshots automatically. What’s more, it will prepare a metadata file, which will contain the frame positions of all localized text on the screen. And that could be a huge help for the translator!
To do that, you need to prepare UI tests from which you will collect the screenshot files as a XCTAttachment. The next step is to prevent Xcode from deleting the test results after successful execution. The easiest way for this is to use a new test plan feature, where we can define a special plan for the localization purpose only. This will prevent storing redundant files during a regular development.
Every year we get some new tools to improve in the app translation process. Xcode 9 introduced string dictionaries, next one improved the exporting flow by introducing Xcode Localization Catalog, and the Xcode 11 improved both features. It is worth following all the flow improvements, because the less we need to do by ourselves, the better. And Apple definitely wants to help us with that.