Cocoa Programming for Mac OS X - Aaron Hillegass [75]
modalDelegate:self
didEndSelector:@selector(alertEnded:code:context:)
contextInfo:NULL];
}
This method will start the sheet. When the user clicks a button, the document object will get sent the message alertEnded:code:context:
- (void)alertEnded:(NSAlert *)alert
code:(NSInteger)choice
context:(void *)v
{
NSLog(@"Alert sheet ended");
// If the user chose "Remove", tell the array controller to
// delete the people
if (choice == NSAlertDefaultReturn) {
// The argument to remove: is ignored
// The array controller will delete the selected objects
[employeeController remove:nil];
}
}
Open RMDocument.xib. Control-drag from the Remove button to the File’s Owner icon to make the File’s Owner be the new target. Set the action to removeEmployee: (Figure 15.5).
Figure 15.5. Change Target and Action of Remove Button
Build and run your application.
Challenge
Add to the Alert sheet a button that says Keep, but no raise. Instead of deleting the employees, this button will simply set the raises of the selected employees to zero.
Chapter 16. Localization
If the application you create is useful, you will want to share it with all the people of the world. Unfortunately, we don’t all speak the same language. Suppose that you wish to make your RaiseMan application available to French speakers. We would say, “You are going to localize RaiseMan for French speakers.”
If you are creating an application for the world, you should plan on localizing it for at least the following languages: English, French, Spanish, German, Dutch, Italian, and Japanese. Clearly, you do not want to have to rewrite the entire app for each language. In fact, our goal is to ensure that you don’t have to rewrite any Objective-C code for each language. That way, all the nations of the world can use a single executable in peace and harmony.
Instead of creating multiple executables, you will localize resources and create string tables. Inside your project directory, an en.lproj directory holds all the resources for English speakers: XIB files, images, and sounds. To localize the app for French speakers, you will add a fr.lproj directory. The XIBs, images, and sounds in this directory will be appropriate for French speakers. At runtime, the app will automatically use the version of the resource appropriate to the user’s language preference.
What about the places in your application where you use the language programmatically? For example, in RMDocument.m, you have the following line of code:
NSAlert *alert = [NSAlert alertWithMessageText:
@"Do you really want to remove these people?"
defaultButton:@"Remove"
alternateButton:@"Cancel"
otherButton:nil
informativeTextWithFormat:@"%d people will be removed.",
[selectedPeople count]];
That Alert sheet is not going to bring about world peace. For each language, you will have a table of strings. You will ask NSBundle to look up the string, and NSBundle will automatically use the version appropriate to the user’s language preference (Figure 16.1).
Figure 16.1. Completed Application
Localizing a NIB File
In Xcode, open RMDocument.xib and bring up the File Inspector. Click the + button under the Localization section (Figure 16.2).
Figure 16.2. Create a French Version of RMDocument.xib
You will be prompted for a locale. Choose French.
If you look in Finder, you will see that a copy of en.lproj/RMDocument.xib has been created in fr.lproj. You will francophize this copy. In the Xcode Project Navigator, you will have two versions of RMDocument.xib: English and French, as shown in Figure 16.3. Click on the French version to open it in the editor.
Figure 16.3. Two Versions of MainMenu.xib
Make your window look like Figure 16.4.
Figure 16.4. Completed Interface
To type in characters with accents, you will need to use the Option key. For example, to type é, type the e, while holding down the Option key, and then type e again. In Mac OS X Lion you can also hold down a key for a second to