Localizing your iOS App | Xcode 12, Swift 5.3
In this short tutorial, we will learn how to display a user interface to support different languages (localizing). Let’s get started!
Step 1: Start a new Xcode project & create the Localizable.strings file
Go ahead and start a new Xcode project, let’s call it languages!
Now, left click — New File, select string file under iOS target and call it Localizable.strings
Make sure to name it properly (Localizable.strings), otherwise, it won’t work.
Step 2: Add languages to the target
Go into your project target, under Localizations, click on the plus button and add a language as seen below. In this example, we will use French.
Now go to your file Localizable.strings > left panel > and click Localize > Select French > click again on both English and French in the left panel.
After that, you will see two files under Localizable.strings:
- Localizable.strings (French)
- Localizable.strings (English)
Step 3: Let’s add some text!
Let’s add a label so we can translate what’s inside our previously created files. Copy/paste the code below into your ViewController:
As you can see, we added a simple UILabel in the middle of our screen, with the text “Hello”. Important here: we cast it on NSLocalizedString so the system is able to localize our string and translate it into the word that will be inside the Localizable.strings (French) after.
Step 4: let’s do some translations
Go into Localizable.strings (French) and copy/paste the code below:
Note: You have to separate each translation with a comma.
Step 5: Change language in settings and run your app
Open your Xcode simulator and go to Settings > General > Language & Region > Add language … > Add French and select it as the main language.
Now run your app and you will see “Bonjour” in the middle of the screen. If you switch back to English in the settings section you will see “Hello”.
Now, you can have fun and translate all the strings present in your app and translate it into as many languages as you want — simply add the language that you liked in the project target and start translating!
Thanks for reading! If you have any questions, leave me a comment and if you find it useful, share it with someone who might also find it interesting!
I’m available for consulting engagements, user experience (UX) Design and iOS app development. Email me at hello@sullivandecarli.com