Translating Strings - iPhone i18n tutorial part 2
From the previous chapter you know how to translate anything that's immediately visible in your interface, such as labels, buttons and even images. But what about messages that pop up, or button labels that change, or any kind of string that exists in your code only?
Dealing with text that comes up later
Find all your strings in your program and change them to the following form:
NSLocalizedString( @"Music", @"The menu item Music." );
NSLocalizedString( @"Films", @"The button Films." );
If you need to include a variable somewhere, use this form:
int titles = 142;
[NSString stringWithFormat: NSLocalizedString( @"We currently have %d movie titles.", @"Movie statistics" ), titles];
It is important NOT to do this differently, e. g. translating two substrings of "We currently have" and "movie titles". This would cause enormous difficulties in languages with a different word order, for example in Japanese you would have to say "Currently we %d movie titles have". By using %d to indicate the position of the number within the string, professional translators will know to shift it around as needed for a flawless translation. By the way, to insert a string rather than a number, use %s.
Now you'll need to leave your nice familiar XCode environment. First, make sure all your project files are saved and jump into the jungle of the Terminal. Sorry, there's no way around it, that's how it's done. You can find Terminal.app under /Applications/Utilities. Now type cd and then a space, but don't push Enter yet. [For those of you not familiar with the command line, this is short for "change directory".] Find your project folder in Finder and drag it into the Terminal window. Notice that the project's path has now dropped into the window. Press enter.
Luckily, there's a command to auto-generate the base language file we need to localize our strings into multiple languages. Just run the following command:
genstrings ./Classes/*.m
After an instant, the Localizable.strings file will be generated in your project's main directory. Drag this file into the Resources directory, but don't click the Add button yet. There are two potential traps to notice. First, this file already exists in the directory, so you don't need to check Copy items into destination group's folder since it already exists there. Second, the text encoding on this file is Unicode (UTF-16) as opposed to the usual Unicode (UTF-8). This will also potentially allow for Asian language support. So, make sure the Copy checkbox is unchecked and that you have selected Unicode (UTF-16) and click Add.
Look at Localizable.strings. If everything was done correctly, you should see the following:
--------
/* The button Films. */
"Films" = "Films";
/* The menu item Music. */
"Music" = "Music";
--------
Notice that the translatable phrases have automatically been put in alphabetical order for you. Now make the file localizable as you did with NIB files and images and add German (de) and Japanese (ja) to the list. Now, under the de directory, change the file in the following way:
--------
/* The button Films. */
"Films" = "Filme";
/* The menu item Music. */
"Music" = "Musik";
--------
Now change the Japanese file to the following:
--------
/* The button Films. */
"Films" = "映画";
/* The menu item Music. */
"Music" = "音楽";
--------
That's it! When working with a translator, and we highly recommend you do, you'd give him the file of course, and that's where the comments will become really useful to improve the quality of translation. Keep in mind though that this file does not contain the interface texts or the app store description, so you have to pass these on separately.