iOS Localization Tutorial
Localization is the process of creating your app support other languages. In many cases, first you make your app with English user interface and then localize the app to other languages such as Japanese.
The process of localization is cyclic, and steps of it modify small by small as XCode obtain updated. In this blog I am going to illustrate the complete steps based on the latest XCode version.
Ensure that you have “Use Base Internationalization” checkmark chosen. Before starting localization work,
What is “Base Internationalization” ?
When you build new XCode project, XCode will automatically makes resources and the file structure comprising them for the default language.
This is so-called “Base” language. If you are creating your app for worldwide market, usually you want to use English texts in this “Base” language resource.
Including New Localization
Ok, by default we have the Base language resource structure. Let’s include new language support.
Choose your project file in Project Navigator, and choose your project in the project and targets list. Open Info tab, and click “+” button under Localizations section. Then select a language you want to support from the dropdown list below.
XCode releases a dialog displaying resources to be included for the new language. Pressing the Finish button will create these files under the new language project folder named [New Language].lproj. (In the following example I included Japanese support, so ja.lproj folder is created.)
At this instant, we have a file structure as below in the project folder.
Where is Localizable.strings file?
Localizable.strings file is where you include translation data as key-value pairs.
Previous versions of XCode used to produce a Localizable.strings file by default, and we were capable to simply duplicate the Localizable.strings file for further languages.
Latest versions of XCode doesn’t make Localizable.strings file by default.
To include Localizable.strings file, go to File->New->File, select Strings Fileunder Resource tab of iOS, name it Localizable.strings , and make the file.
Currently, you have a Localizable.strings file for Base language as shown.
To include Localizable.strings for Japanese, tap Japanese in the File Inspector. This will build a new Localizable.strings file under the ja.lproj folder.
Now we have two Localizable.strings files — one under the Base.lproj folder and the other one under the ja.lproj folder.
Let’s include words and phrases used in the app to the Localizable.strings file of Base.
The below examples explains where I included “Welcome” = “Welcome”;
The left hand side is known as Key which is later used by NSLocalizedString method to drag out the text in the right hand side. This is key-value pair kind of data.
Example of NSLocalizedString method is shown in the below image. We place a key as the first parameter of the method. By identify the key as the first parameter, the method can pull out the corresponding value from the Localizable.stringsfile and returns the value. In the below example, I find localized strings for alert title, message, and buttons.
let alertTitle = NSLocalizedString(“Welcome”, comment: “”)
let alertMessage = NSLocalizedString(“Thank you for trying this app, you are a great person!”, comment: “”)
let cancelButtonText = NSLocalizedString(“Cancel”, comment: “”)
let signupButtonText = NSLocalizedString(“Signup”, comment: “”)
let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertControllerStyle.Alert) let cancelAction = UIAlertAction(title: cancelButtonText, style: UIAlertActionStyle.Cancel, handler: nil) let signupAction = UIAlertAction(title: signupButtonText, style: UIAlertActionStyle.Default, handler: nil) alert.addAction(cancelAction) alert.addAction(signupAction) presentViewController(alert, animated: true, completion: nil)
Run the app, and we should observe the alert with English text.
As a next step, we include Japanese texts to a Localizable.strings file under the ja.lproj folder like mentioned below. We utilize the similar keys but replace values with corresponding Japanese translations.
Following this, switch the phone language to Japanese on the iOS simulator, and run the app, you should observe the alert with Japanese texts now.
It is work some to switch your phone language each time you verify localization results. XCode has a good feature to control languages only within the app when you run the app on iOS Simulator.
To perform so, choose Edit Scheme from the dropdown on the upper left corner of the XCode window, and modify Application Language from System Language to Japanese (Refer the below image if you got lost).
This design doesn’t modify the phone language of the simulator, but only modify the language environment within the app to the specified language. This is suitable when you include a few languages and desire to switch between languages to verify your localization result.
Ok, currently we recognize how to pull localized texts using NSLocalizedString and how to set up data in Localizalbe.strings files.
This is sufficient for you to programmatically illustrate localized texts to users.
The next step sustaining localization of texts which are place in Storyboards (like button titles) . If you set button titles or label texts in Storyboards and don’t modify those texts programmatically in ViewControllers, you have to do localization on your Storyboard files.
To include translation data to the words used in Storyboards, first select the Storyboard file from the Project Navigator, then choose and include Japanese in the File Inspector on the right. This creates [StoryboardFileName].stringsfile under the ja.lproj folder. In the below mentioned example, as the Storyboard file name is Main.storyboard, Main.strings (Japanese) is formed.
In Main.strings file, you will observe impressive like below.
/* Class = "UIButton"; normalTitle = "Get Started"; ObjectID = "qs4-6I-gUp"; */ "qs4-6I-gUp.normalTitle" = "Get Started";
Substitute “Get Started” part of the line above to the corresponding Japanese phrase like,
/* Class = "UIButton"; normalTitle = "Get Started"; ObjectID = "qs4-6I-gUp"; */ "qs4-6I-gUp.normalTitle" = "始める";
Run the app. The title of the button should be localized to Japanese well
Pain to manage [Storyboard].strings files
One problem is that Main.strings file won’t be updated when you include new UI components on the Storyboard file.
So you always have to include UI components first and then make Main.strings by turning on Japanese localization in the File Inspector.
Localizing App Title
To localize app title or other gears definite in Info.plist file, create a InfoPlist.strings file.
Navigate to File->New->File , choose Strings File under Resource tab of iOS , name it InfoPlist.strings. Choose the Base.lproj folder as the location of InfoPlist.strings file. (By doing this, this InfoPlist.strings file is standard as the one for the base language by XCode.)
Generally we are concerned in localizing these two values in info.plist file.
CFBundleDisplayName — App name display on the home screen
NSHumanReadableCopyright — Copyright description (e.g. 2014 Goldrush Computing Inc. All right reserved)
Situate an app name and copyright for these two keys like the following.
/* InfoPlist.strings LocalizationTutorialApp Created by Takamitsu Mizutori on 2016/07/25. Copyright © 2016年 Goldrush Computing Inc. All rights reserved. */ "CFBundleDisplayName" = "MyApp"; "NSHumanReadableCopyright" = "2016 Goldrush Computing Inc. All rights reserved.";
Then in the File Inspector, ensure Japanese to include an InfoPlist.strings below the ja.lproj folder (you have to stay the InfoPlist.strings file chosen when doing this).
In the InfoPlist.strings (Japanese) file, change the values with Japanese translation as below.
/* InfoPlist.strings LocalizationTutorialApp Created by Takamitsu Mizutori on 2016/07/25. Copyright © 2016年 Goldrush Computing Inc. All rights reserved. */ "CFBundleDisplayName" = "マイアプリ"; "NSHumanReadableCopyright" = "2016年 Goldrush Computing Inc. All rights reserved.";
Run the app and observe if your app title is properly localized to Japanese.
This is the complete process of localizing an app to another language. if you need any localization for your business, feel free to contact us.