您的位置:首页 > 其它

Cocoa Programming for Mac OS X 第十六章(Localization)摘录

2011-08-22 15:17 489 查看
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


[View full size image]




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


[View full size image]



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.nib: English 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


[View full size image]



Make your window look like Figure 16.4.


Figure 16.4. Completed Interface


[View full size image]



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


[View full size image]



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


[View full size image]



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


[View full size image]



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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: