How to localize an iOS app programmatically (made easy)
--
Sometimes we want our apps to support multiple languages to reach a higher audience.
This is when we turn to localization.
Localization vs Internationalization in CS
- Localization is the process of adapting the software to support different languages, time zones, and respect laws and cultures of different regions and locales
- Internationalization is the process of designing a software that meets the needs of the users all around the world, for instance: designing an app that adapts to the RTL languages.
Localization is about the content.
Internationalization is about the UI.
Localizing our app
Localizing our app consists of 2 basic steps:
- Creating strings that can be localized (
NSLocalizedString
); - Physically localizing and translating our strings;
Creating strings that can be localized
In order to localize strings, we have to use NSLocalizedString
.
We declare a NSLocalizedString
like this:
let hello = NSLocalizedString("hello", comment: "greeting")
Where the first string (hello) is the one we want to be displayed and the second (“greeting”) is the description of the first.
It’s clear that when we have a medium/big sized project the process of writing all those NSLocalizedString
s is so ANNOYING! Especially when the meaning of the string is so obvious or we just don’t care about giving it a descriptive comment.
So, in those cases I like helping myself by using the following String extension :
localize
is an attribute we can call on String instances to transform them in NsLocalizedString
with both content and comment set to self
, therefore, set to the string we call it on. What I just said, translated to code is much easier. It looks like this:
let helloStr = "Hello!".localize //helloStr now contains…