Friday, March 17, 2017

Steps to localize ios app in xcode including storyboard, plist file, code

In xcode 8, the internationalization setting is in the project (not target)'s info settings, which is shown below.


Step 1: localize string in storyboard
1. Be sure the "Use Base Internationalization" checkbox is checked
2. Check the storyboard to delete all unused string, otherwise they will appear in the localized string files
3. click the language + button to add a new language, like Chinese, which will create new string file for the added language under the storyboard folder. This file contains all the strings used in the storyboard. Translate the string to the proper language.
4. Change the device language to the new language, and restart the app, the localized string should show in the storyboard UI elements


Step 2: localize strings in plist file
The second step is localized the application display name as well as other strings defined in plist file
1. from the xcode's File->New->File, select Resource tab, and create a string file, the file name is
InfoPlist, (the full file name is InfoPlist.strings)
2. select the InfoPlist file, and in xcode identity inspector window, click localization button, and then check the language you want to localize for the plist file
3. Open the localized InfoPlist file as property list, and add the key value pairs you want to localized, the key must match the keys defined in the original info.plist file, for example, to localize the display bundle name, add the below item
Note you only need to add the key/value pairs in localized InfoPlist file, and there is no need to make any change in InfoPlist file in base.lproj folder, as if the value cannot be found in there, it will use the value defined in the info.plist file.
Likely, only bundle display name and feature usage permission description (like NSCameraUsageDescription etc.) need to be localized in info plist file
After the display bundle name is changed, test it on device, the application name shown in the device home screen should have the localized name.

Step 3: localize hardcoded string in source file
1. go through the swift source files and identify all the strings that will appear in the UI, replace the hardcoded string by calling NSLocalizedString method with any comment  as below
self.navigationItem.title = "New Password"
to
self.navigationItem.title = NSLocalizedString("New Password", comment: "New Password screen title")

The comment parameter does not matter, and it will only show as comment in the string file generated in the last step. Actually, just set it to empty string "".

2. from terminal app, goto the application project folder, and run the below command:
find . -name \*.swift | xargs genstrings -o .
This will generate a file localizable.strings under this folder
3. add the generated file into xcode project, 
4. select the file from xcode identity inspector, and click localization button to localize it for different language
5. translate the localized files for all the keys
6. test the app under different language and verify the ui is updated to different language settings

3 comments:

  1. Hello Jonathan! If you need to localize iOS app, maybe you would be interested in checking out a translation management platform like https://poeditor.com to automate the process. Great tool for collaborative projects. Worth a try.

    ReplyDelete
  2. This blog gives very important info about iOS Thanks for sharing
    iOS App Development Online Training

    ReplyDelete
  3. Thanks a lot for sharing this amazing knowledge with us. This site is fantastic. I always find great knowledge from it. Apple Developer Enterprise Account

    ReplyDelete