Localize a SwiftUI app
Boost app engagement and market reach through effective and easy localization.
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
Add a new language using the bottom button.
Build your project to see the list of strings you prepared in the earlier step.
Edit the strings for the new language using the editor.
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
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
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.