How to Use and Automate Localizable Strings in Xcode with SwiftUI

Marcelo
3 min readApr 29, 2023

--

Introduction: In this tutorial, we’ll guide you through the process of using localizable strings in Xcode with SwiftUI. Localizing your app is essential to cater to a global audience and provide a better user experience by presenting content in their native language. We’ll cover creating and managing localization files, using localizable strings in SwiftUI, and testing localization in the iOS simulator.

Prerequisites:

  • A basic understanding of Swift and SwiftUI
  • Xcode installed on your Mac

Step 1: Create a new SwiftUI project

  1. Launch Xcode and create a new SwiftUI project.
  2. Name your project, select a location, and click “Create”.

Step 2: Add localization languages to your project

  1. In the Project Navigator, select your project.
  2. In the “Info” tab, under “Localizations”, click the “+” button.
  3. Choose the languages you want to support and click “Finish”.

Step 3: Create a Localizable.strings file

  1. In the Project Navigator, click “File” > “New” > “File”.
  2. Select “Strings File” under “Resource” and click “Next”.
  3. Name the file “Localizable.strings” and click “Create”.
  4. Select the file in three files and in the right of the screen click on the "Localize" button inside Localization section.
  5. Within the folder tree on the left side of the screen, click on the topmost folder (1) that has the name of your project. After that, select the first target (2). Enter the Info menu (3), and in Localizations (4) is where you will add the languages you want to support in your app.

Step 4: Add localized strings to Localizable.strings files

  1. Select the “Localizable.strings” file in the Project Navigator.
  2. You’ll see multiple files for each language you’ve added. Edit each file and add your key-value pairs for the localized strings.
"key" = "Localized String";

For example, for English localization:

"welcome_message" = "Welcome to our app!";
"welcome_button" = "Let's go";

For French localization:

"welcome_message" = "Bienvenue dans notre application!";
"welcome_button" = "Allons-y!";

All your Localizable Files will need to have the same key because your app will use the text of these files and not directly what you use on code.

Step 5: Use localizable strings in SwiftUI

  1. In your SwiftUI views, use the Text component with the LocalizedStringKey initializer to display localized strings. Pass the key as a parameter:
Text(LocalizedStringKey("welcome_message"))

Step 6: Test localization in the iOS simulator

  1. To test your localization, you need to change the language of your iOS simulator.
  2. Launch the iOS simulator.
  3. Go to “Settings” > “General” > “Language & Region” > “iPhone Language” and select the desired language.
  4. Run your app in the simulator to see the localized content.

Congratulations! You’ve successfully implemented localizable strings in your SwiftUI app using Xcode.

Want help to translate all your localizable strings faster? Check this out now:

https://apps.apple.com/us/app/langify/id6448647989

I developed a Mac app called Langify. In it, you paste all of your localizable strings from your primary language, and the app will translate them all and give you the formatted text to paste into the files for other languages.

First, you input your keys with their values in your language and choose which language you want to translate them into. Currently, it’s possible to translate from any language to English, Portuguese, or Spanish.

That's all guys!

Feel free to follow me on Twitter (https://twitter.com/Marceloproducts) and on Instagram (https://instagram.com/pohmarcelo)

My website: https://marcelodiefenbach.com.br/

Langify: https://apps.apple.com/us/app/langify/id6448647989

--

--