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.
- A basic understanding of Swift and SwiftUI
- Xcode installed on your Mac
Step 1: Create a new SwiftUI project
- Launch Xcode and create a new SwiftUI project.
- Name your project, select a location, and click “Create”.
Step 2: Add localization languages to your project
- In the Project Navigator, select your project.
- In the “Info” tab, under “Localizations”, click the “+” button.
- Choose the languages you want to support and click “Finish”.
Step 3: Create a Localizable.strings file
- In the Project Navigator, click “File” > “New” > “File”.
- Select “Strings File” under “Resource” and click “Next”.
- Name the file “Localizable.strings” and click “Create”.
- Select the file in three files and in the right of the screen click on the "Localize" button inside Localization section.
- 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
- Select the “Localizable.strings” file in the Project Navigator.
- 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
- In your SwiftUI views, use the
Textcomponent with the
LocalizedStringKeyinitializer to display localized strings. Pass the key as a parameter:
Step 6: Test localization in the iOS simulator
- To test your localization, you need to change the language of your iOS simulator.
- Launch the iOS simulator.
- Go to “Settings” > “General” > “Language & Region” > “iPhone Language” and select the desired language.
- 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:
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!
My website: https://marcelodiefenbach.com.br/