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

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

概述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 ap

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,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.nibEnglish and french,as shown in @L_403_22@. 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 ee 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,monospace; Font-size:14px">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]


@L_404_62@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,monospace; Font-size:14px">u. To 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 genstringsNSLocalizedString and creates a skeleton string table. This string table includes the comment.)

In Mydocument.mname="place%20where" DELETE",@"Delete") defaultbutton:NSLocalizedString(@"DELETE",@"Delete") alternatebutton:NSLocalizedString(@"CANCEL",@"Cancel") otherbutton:nilinformativeTextWithFormat: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

总结

以上是内存溢出为你收集整理的Cocoa Programming for Mac OS X 第十六章(Localization)摘录全部内容,希望文章能够帮你解决Cocoa Programming for Mac OS X 第十六章(Localization)摘录所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1066400.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存