Cocoa Programming for Mac OS X 第十六章(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, anEnglish.lproj directory holds all the resources for English speakers: nib files, images, and sounds. To localize the app for French speakers, you will add a French.lproj directory. The nibs, 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 MyDocument.m, you have the following line of code:

NSAlert *alert = [NSAlert alertWithMessageText:@"Delete
             defaultButton:@"Delete"
           alternateButton:@"Cancel"
               otherButton:nil
 informativeTextWithFormat:@"Do you really want to delete %d people?",
                                    [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 askNSBundle to look up the string, and it 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, select—but do not open—MyDocument.nib, and bring up the Info panel. Click the Add Localization button (Figure 16.2).

Figure 16.2. Create a French Version of MyDocument.nib


You will be prompted for a locale. Choose French.

If you look in Finder, you will see that a copy of English.lproj/MyDocument.nib has been created in French.lproj. You will francophize this copy. In Xcode, under the Resources group, you will have two versions of MyDocument.nibEnglish and French, as shown in Figure 16.3. Double-click on the French version to open it in Interface Builder.

Figure 16.3. Completed Application


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 the International page of System Preferences, you can add the Keyboard Viewerto 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 nib files (the French version and the English version). 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 International page of the System Preferences application. Set Français 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 simply 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 = [NSBundle mainBundle];
NSString *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 have just one string table—Localizable.strings—for each language.

Creating String Tables

To create a Localizable.strings file for English speakers, choose the New File... menu item in Xcode. Create an empty file, and name it Localizable.strings. Save it in the English.lproj directory (Figure 16.5).

Figure 16.5. Create an English String Table


Edit the new file to have the following text:

"DELETE" = "Delete";
"SURE_DELETE" = "Do you really want to delete %d people?";
"CANCEL" = "Cancel";

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

Now create a localized version of that file for French. Select the Localizable.strings file in Xcode, bring up the Info panel, and create a localized variant (Figure 16.6).

Figure 16.6. Create a French String Table


Edit the file to look like this:

"DELETE" = "Supprimer";
"SURE_DELETE" =
      "Etes-vous sûr de vouloir effacer ces %d personnes ?";
"CANCEL" = "Annuler";

(To create the u with the circumflex, type i while holding down the Option key, and then type u. To type é, type e while holding down the Option key, and then type e again.)

When saving a file with unusual characters, you should use the Unicode (UTF-8) file encoding. In the Info panel forFrench.lproj/Localizable.strings, set the file encoding to UTF-8. When you are presented with a panel asking whether you wish to convert the file to UTF-8, click the Convert button (Figure 16.7).

Figure 16.7. Change the File Encoding


Save the file.

Using the String Table

In an app with only one string table, you would probably do this:

NSString *deleteString;
deleteString = [[NSBundle mainBundle]
                    localizedStringForKey:@"DELETE"
                                    value:@"Delete?"
                                    table:nil];

Fortunately, there is a macro defined in NSBundle.h for this purpose:

#define NSLocalizedString(key, comment)
          [[NSBundle mainBundle] localizedStringForKey:(key)
                                                 value:@""
                                                 table:nil]

(Note that the comment is completely ignored by this macro. It is, however, used by a tool called genstrings, which scans through your code for calls to the macro NSLocalizedString and creates a skeleton string table. This string table includes the comment.)

In MyDocument.m, find the place where you run the Alert panel. Replace that line with this one:

NSAlert *alert = [NSAlert
     alertWithMessageText:NSLocalizedString(@"DELETE", @"Delete")
            defaultButton:NSLocalizedString(@"DELETE", @"Delete")
          alternateButton:NSLocalizedString(@"CANCEL", @"Cancel")
              otherButton:nil
informativeTextWithFormat:NSLocalizedString(@"SURE_DELETE",
                          @"Do you really want to delete %d people?"),
                           [selectedPeople count]];

Build the app. Change your preferred language back to French in System Preferences, and run the app again. When you delete a row from the table, you should get an Alert panel in French

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值