Localize a SwiftUI app

Localize a SwiftUI app

Boost app engagement and market reach through effective and easy localization.

·

3 min read

App localization is crucial when tailoring an app to different regions and locales. Localization boosts user engagement, increases accessibility, and can significantly expand an app’s market reach.

I had concerns that localizing Clean Share (app for removing tracking identifiers) to German would be a painful process but found that SwiftUI and advancements in Xcode makes this straightforward. Follow along to see how easy it is 🎉

Prepare Strings

This can be done in two ways.

Component Initializer

Many SwiftUI components come with an initializer that takes a LocalizedStringKey. This allows you to use a string literal.

Text("Hello")
Toggle("Is On", isOn: ...)
Button("Help", ...)

String Initializer

If you have a string that is passed around, you can use the localized initializer.

String(localized: "Buy a book")

It is also recommended to give a comment to provide context and assist localizers.

Text("Hello", comment: "The title of the home screen.")
String(localized: "Hello", comment: "The title of the home screen.")

String Catalog

An Xcode string catalog is a file format used to manage localized strings. It allows developers to store all the translations for different languages in a structured format, making it easier to manage and update text in multi-language applications.

Create a String Catalog via File > New > File > String Catalog

An Xcode screenshot showing the new file template with a string catalog file selected and highlighted.

Add a new language using the bottom button.

An Xcode screenshot of the string catalog editor with a callout for the Add button near the bottom of the Language pane.

Build your project to see the list of strings you prepared in the earlier step.

An Xcode screenshot of the string catalog editor showing a newly added language setting with a 0 percent indicator beside it.

Edit the strings for the new language using the editor.

An Xcode screenshot showing the Project navigator on the left with a string catalog selected. The string catalog editor on the right contains English strings with Swedish translations.

Plurals

Languages have different rules for pluralizing nouns based on their specific grammatical structures. For example, English uses "1 Book" for singular and "X Books" for plural, while other languages may vary.

To support this, use string interpolation:

Text("\(bookCount) Books")

After building again, select the String catalog > Right Click Row > Vary By Plural

An Xcode screenshot of the string catalog editor showing one of the localized strings selected with a contextual menu. The Vary by Plural option is selected.

Vary by Device

It is also possible to vary strings by device. For example on smaller devices you may want to display less content or specify a particular interaction (tap vs click). See the screenshot above for the button location.

Testing Translations

You can test your translations in two ways:

Scheme

Product > Scheme > Edit Scheme > Run > Options > App Language

An Xcode screenshot showing an open scheme editor with the Swedish language selected in the App Language drop-down.

Settings

Open the Settings app on device and either change the device language or for the particular app.

Conclusion

Localizing apps has never been easier with the addition of String catalogs and SwiftUI. Combine this with translations through an LLM and you can perforn localization very quickly. This allowed me to localize Clean Share to German in less than a day. See how it works for you ❤️

Further reading in Apple's documentation.