Online Book Reader

Home Category

Cocoa Programming for Mac OS X - Aaron Hillegass [76]

By Root 829 0
see a popup with its diacritical marks (In the International page of System Preferences, you can add the Keyboard Viewer to your input menu. If you are using a lot of unusual characters, the Keyboard Viewer can help you learn which key combinations create which characters.)

At this point, you have created a localized resource. Note that if you make a lot of changes to your program, you may need to update both XIB files (the French version and the English version). For example, finding that the buttons were too narrow for the French translation, Aaron resized the window accordingly. For this reason, it is a good idea to wait until the application is completed and tested before localizing it.

Build your app. Before running it, bring up the Language & Text page of the System Preferences application. Drag Français to the top of the list to set it as your preferred language. Now run your application. Note that the French version of the NIB is used automatically.

Also note that the document architecture takes care of some localization for you. For example, if you try to close an unsaved document, you will be asked in French whether you want to save the changes.

String Tables


For each language, you can create several string tables. A string table is a file with the extension .strings. For example, if you had a Find panel, you might create a Find.strings file for each language. This file would have the phrases used by the Find panel, such as “None found.”

The string table is just a collection of key-value pairs. The key and the value are strings surrounded by quotes, and the pair is terminated with a semicolon:

"Key1" = "Value1";

"Key2" = "Value2";

To find a value for a given key, you use NSBundle:

NSBundle *main;

NSString *aString;

main = [NSBundle mainBundle];

aString = [main localizedStringForKey:@"Key1"

value:@"DefaultValue1"

table:@"Find"];

This would search for the value for "Key1" in the Find.strings file. If it is not found in the user’s preferred language, the second-favorite language is searched, and so on. If the key is not found in any of the user’s languages, "DefaultValue1" is returned. If you do not supply the name of the table, Localizable is used. Most simple applications just have one string table for each language: Localizable. strings.

Creating String Tables


To create a Localizable.strings file for English speakers, choose the New -> New File... menu item in Xcode. In the Mac OS X Resource category, create a new Strings file, and name it Localizable.strings. Save it in the en.lproj directory (Figure 16.5).

Figure 16.5. Create an English String Table

Edit the new file to have the following text:

"REMOVE_MSG" = "Do you really want to remove these people?";

"REMOVE_INF" = "%d people will be removed.";

"REMOVE" = "Remove";

"CANCEL" = "Cancel";

Save it. (Don’t forget the semicolons!)

Now create a localized version of that file for French. Select the English version of the Localizable.strings file in Xcode (it is the only version so far), bring up the File Inspector, and create a localized variant (Figure 16.6).

Figure 16.6. Create a French String Table

Edit the file to look like this:

"REMOVE_MSG" = "Voulez-vous supprimer ces personnes?";

"REMOVE_INF" = "%d personnes seront supprimées.";

"REMOVE" = "Supprimer";

"CANCEL" = "Annuler";

(To create the e with the accent aigu, type e while holding down the Option key, and then type e.)

When saving a file with unusual characters, you should use the Unicode (UTF-8) file encoding. In the File Inspector for fr.lproj/Localizable.strings, ensure that the Text Encoding is set to UTF-8. (If it is not and you are presented with a panel asking whether you wish to convert the file to UTF-8, click the + button labeled Convert.)

Save the file.

Using the String Table


In an app with just one string table, you would write code like this a lot:

NSString *deleteString;

deleteString = [[NSBundle mainBundle]

localizedStringForKey:@"REMOVE"

value:@"Do you want to remove these

Return Main Page Previous Page Next Page

®Online Book Reader