2009-10-30

internationalizing an existing iPhone app

To make your app work in different languages you need to set the app to handle different languages, and then create the translations from the app's native language to other languages.

Here's one way to do that, which avoids keeping track of multiple .xib files and instead puts all the strings for an app in one file:
1) set all your labels and button titles from your code
2) make all the strings in your code be NSLocalizedString values
3) create a Localizable.strings file, add it to your project and make it localizable.
4) translate the Localizable.strings files

Here are the details.
1) set all your labels and button titles from your code
This straightforward coding. For each label and button in your xib files, create a corresponding UILabel or UIButton as an IBOutlet, connect the variable to the interface item, and use the UILabel's text property or the UIButton's setTitle method to set the titles.

I recommend making the titles in a xib have square brackets around them so you can know whether the label's coming from the code or the xib.

2) make all the strings in your code be NSLocalizedString values
Replace hardcoded strings with NSLocalizedString. For example, instead of

labelFriction.text = "Friction";

use

labelFriction.text = NSLocalizedString(@"Friction", @"label for the Friction slider" );

The second string, @"label for the Friction slider" in this example, gives direction to translators so they know the context of the string they're translating. If you use the same string more than once, be sure to use the same comment, too, or the genstrings tool will throw warnings.

You can Find in Project the string @" (that is, at-sign, double-quote) to find all the strings in your project. For any string that's not a file name or in an NSLog statement, make sure the string is an NSLocalizedString.

3) create a Localizable.strings file, add it to your project and make it localizable.
Create this fie with the genstrings app.

  • launch Terminal.app

  • cd to your project's folder

  • run genstrings with this command,
    genstrings ./Classes/*.m
    You should run this from the project folder (above /Classes) because where you run it is where the Localizable.strings file will be created and you want that in the project folder, not the /Classes folder.


Now that the file's there, add it to your project. In Xcode, ctl-click on your Resources folder and Add / Existing files... Select Localizable.strings and click Add. Change the Text Encoding to Unicode (UTF-16) and click Add.

Then, make Localizable.strings localizable. Bring up the Info window for Localizable.strings and click the Make File Localizable button. Then, in the General tab of the Info window click the Add Localization and enter the 2-character locale code for the language, which you can find at http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes

Each new language will result in a new folder in your project.

4) translate the Localizable.strings files
Translate the Localizable.strings for each language you want to support, and put the translated copies in the appropriate folders of your project.

0 comments:

Post a Comment