Localization is the process of making your app support other languages. Xcode has build-in support for localizations.
Step 1: Internationalize your app
Select your project, under Info tab, you’ll see a heading Localizations. Under it, check the option labelled “User Base Internationalization”. That’s all for this step.
Step 2: Add a language
In the same place as the above step, click on the ‘+’ button and choose a language.
You will see a pop up showing your Main.storyboard and Launchscreen.storyboard files.
Make sure both are checked and click on Finish.
This will create an extra file each under Main.storyboard and Launchscreen.storyboard.
Open the German Main.strings file. If you have content in your storyboard, you will find all your text here, along with object IDs for reference. Simply replace the texts with the German translations, you’ll have something like this:
/* Class = "UIButton"; normalTitle = "Done"; ObjectID = "0mZ-pG-mPo"; */ "0mZ-pG-mPo.normalTitle" = "Erledigt";
and then run your app after changing the Simulator device language, and you can see the changes.
So this handles all the text in the storyboard and launchscreen. What about the translation in the strings in code? For that we shall create the Localizable.strings file.
Step 3: Create Localizable strings file
Inside your app folder, create a new file. In the dialog, select iOS/Resource/Strings File and click Next. Name the file Localizable and click Create. [Localizable.strings is the default filename iOS uses for localized text]
Select the file you just created, and in the Attribute Inspector, click the button labelled Localize. Select OK in the resulting dialog.
Now in the same place, under Localization, make sure all languages are check-marked. This will create one strings file for each of the languages.
This .strings file will contain any number of key-value pairs, just like a Dictionary. The conventional practice is to use the base language translation as the key. The file has the following format:
"KEY" = "CONTENT";
Note: Unlike Swift, the .strings file requires that each line terminates with a semicolon.
This strings file with .strings file extension to store all of the localized strings used within the app. A simple function call will retrieve the requested string based on the current language in use on the device.
Let us add some text to these files.
In the Base language file, write
“Welcome” = “Welcome”; "How can I help you today?" = "How can I help you today?";
In the German language file,
“Welcome” = “Herzlich willkommen”; "How can I help you today?" = "Wie kann ich Ihnen heute helfen?";
Retrieve these in your swift files in the following way:
let title = NSLocalizedString("Welcome", comment: "") let message = NSLocalizedString("How can I help you today?", comment: "") //present them in an alert controller for demo let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert) presentViewController(alert, animated: true, completion: nil)
Run the app and you should see the alert with English text.
Change the Simulator device language to German and run again, you should see the German text.
Use all your strings in this way, or in the storyboard, and your app is now supporting two langauges!
Localize App Name
To localise the app name, we just need to create a new localised strings file – same as you created the Localizable.strings file in the above step.
- Create a new strings file named InfoPlist.strings,
- Localise it by clicking on the Localize button in Identity inspector
- Checkmark all your languages
Add this line in each language file
CFBundleDisplayName = "Localized App Name";
Run the app with different language settings, the app name under the icon should appear accordingly.