A couple of weeks ago, I wrote an article about problems frequently encountered in iOS localization . Here I’d like to share with you more information about localization. I will talk about easy ways of localizing storyboards,
.xib files and literal strings.
1. Literal Strings
Localizing literal strings is one of the basics you need to pay attention to. Moreover, if you decided to start off building a global app, you’d better localize all the literal strings ground up. This will save you from a lot of troubles and headaches.
The way to localize literal strings, as you may already know, is to use wrap a string with an
Instead of writing
@"Some text", you write
NSLocalizedString(@"Some text", @"Comments for translators");
You can ignore the second text parameter if you wish. The second piece of text actually serves as a comment for translators to better understand the context of your string, so that they can more accurately translate your text as desired.
After wrapping all the literal strings in this way, you can use the command tool to pull out all the strings wrapped, so that you can easily translate the text strings into multiple languages.
In order to do this,
- Open up Terminal.
- Navigate to your project folder.
- Create a folder called
en.lproj if you haven’t already done so.
(You will need to drag the
en.lproj folder into your XCode project as well)
- Generate the strings file, by typing the
genstrings -o en.lproj Classes/*.m command in the Terminal.
Classes/*.m represents all your
.m files. You should change this accordingly.
Now when you open up the
en.lproj folder, you will see a new file called
Localizable.strings was already generated by the Terminal command tools. The format of the file is like:
/* Comments for translators */
"Some text" = "Some text";
By now, you have extracted all the literal strings in your code. Now navigate to the Supporting Files folder (in Xcode). Click on
Localizable.strings and bring up the property panel on the right. Check the languages you want to translate for. (You may need to manually create a [lang].lproj ([lang] is the code for specific languages) folder and copy the
Localizable.strings into the folder, if you can’t check the languages on the right panel.)
You should see
Localizable.strings of several languages appearing on the left. Just translate all the texts and paste them into the
Localizable.strings of the corresponding language.
/* Comments for translators */
"Some text" = "一些文字";
In case you encounter errors like Localizable.strings error, please refer to my post before.
2. Storyboard localization
Since iOS5, Apple has been using the storyboard as the default interface builder. One way to localize the text in storyboard is to create multiple storyboards of different languages and translate the text in the storyboards respectively.
Another simpler way is to make use of the
ibtool to automatically generate the strings for translation (similar to the
genstrings method used above). In order to do this,
- Select and check the desired translation language under the Localization panel on the right. The Project navigator MainStoryboard.storyboard will be split into two different files, one for each language. (You may need to manually build different storyboad files and put them into different
- Open up Terminal and navigate to your project folder.
- Navigate to en.lproj folder in Terminal. (
cd en.lproj) (Make sure that the main storyboard file is in this folder. If not, navigate to the folder containing the main storyboard file.)
- Enter command
ibtool --generate-strings-file MainStoryboard.strings MainStoryboard.storyboard
A file called
MainStoryboard.strings will be generated. This file contains all the text to be translated in the storyboard file.
An example of the file would be like:
/* Class = "IBUILabel"; text = "First Name"; ObjectID = "CkK-0F-oZ2"; */
"CkK-0F-oZ2.text" = "Li";
/* Class = "IBUILabel"; text = "Age"; ObjectID = "M3f-at-Qtm"; */
"M3f-at-Qtm.text" = "13";
/* Class = "IBUILabel"; text = "Last Name"; ObjectID = "SMp-zG-VzS"; */
"SMp-zG-VzS.text" = "Cognom";
Now, translate all of the text into your language and save the file. That done, it’s time to execute another instruction in the Terminal window to regenerate the translated version of our storyboard, but this time with translations for all the texts. Navigate to the en.lproj folder (using Terminal) and enter the following command:
~/.../en.lproj$ ibtool --strings-file MainStoryboard.strings
zh-Hans.lproj should be changed to your own translation folder.
.xib file localization
If you are using seperate
.xib files instead of the storyboard, you can also use the
ibtool to generate the strings for translation. The following example shows you how to extract the English strings from the main nib of a project into a new strings file. You can store a copy of the resulting file for use later or give the file to the translation team.
ibtool --generate-strings-file MainMenu.strings en.lproj/MainMenu.nib
The following example takes the original English-based nib file and merges it with the translated strings to create a localized version of the nib:
ibtool --strings-file de.lproj/MainMenu.strings --write de.lproj/MainMenu.nib en.lproj/MainMenu.nib
Remember that translating the strings in your files is only one step of the localization process. The localizer may need to adjust the layout of views and controls to account for changes in string length. Also,
ibtool does not automatically update date and time formats associated with formatter objects.