xcode4中添加多國語言支援已經與前期的版本方式不太相同,這裡進行簡要的介紹。
一,在你的工程中找到supporting files,在其上右鍵,找到如下 所示的藍色高亮項,單擊。
注意,你已經看到了我生成的多個語言支援檔案,你在沒有添加以前這裡是沒有的!!!!。
二,如下,選擇ios目錄下的資源檔案,找到string檔案選擇。
三,将建立的檔案命名為localizable,這個是一定的,不能命名為其他的名稱。這裡要注意。完成後打開inspector,選中localizable.strings,你可以看到如下所示的界面。
四,找到localization,點選左下角的+号按鈕如下:添加你想要的語言支援。
五,最終生成的語言檔案如下:
此時你将在你的工程檔案下面找到如下所示的檔案目錄:每一個檔案目錄中都有對應的語言檔案支援。現在需要做的就是在這些對應的檔案中添加字元串支援。最終在要設定字元串的代碼中調用nslocalizedstring(@"string2",nil);方法進行字元串的顯示,一切搞寫。
範例如下: localizable.strings (english)
"demotitle"="this is english version";
"string1"="apple";
"string2"="banana";
"string3"="orange";
"string4"="watermelon";
"string5"="strawberry";
localizable.strings (zh_cn)
"demotitle"="這是中文的版本";
"string1"="蘋果";
"string2"="香蕉";
"string3"="橘子";
"string4"="西瓜";
"string5"="草莓";
代碼:
multilanguagedemoviewcontroller.h
#import <uikit/uikit.h>
@interface multilanguagedemoviewcontroller : uiviewcontroller {
iboutlet uilabel *lbldemotitle;
iboutlet uilabel *lblstr1;
iboutlet uilabel *lblstr2;
iboutlet uilabel *lblstr3;
iboutlet uilabel *lblstr4;
iboutlet uilabel *lblstr5;
}
@property (nonatomic,retain) uilabel *lbldemotitle;
@property (nonatomic,retain) uilabel *lblstr1;
@property (nonatomic,retain) uilabel *lblstr2;
@property (nonatomic,retain) uilabel *lblstr3;
@property (nonatomic,retain) uilabel *lblstr4;
@property (nonatomic,retain) uilabel *lblstr5;
@end
multilanguagedemoviewcontroller.m
#import "multilanguagedemoviewcontroller.h"
@implementation multilanguagedemoviewcontroller
@synthesize lbldemotitle,lblstr1,lblstr2,lblstr3,lblstr4,lblstr5;
- (void)viewdidload {
[super viewdidload];
//系統設定取得多國語言字元串
lbldemotitle.text=nslocalizedstring(@"demotitle",nil);
lblstr1.text= nslocalizedstring(@"string1",nil);
lblstr2.text=nslocalizedstring(@"string2",nil);
lblstr3.text=nslocalizedstring(@"string3",nil);
lblstr4.text=nslocalizedstring(@"string4",nil);
lblstr5.text=nslocalizedstring(@"string5",nil);
2. xib檔案國際化
在需要國際化的xib檔案上get info 添加多語言版本,
修改各語言版本相應的界面文字及圖檔。
模拟器測試記得要在模拟器上恢複原廠設定,xcode也要clean一下,否則可能出錯。
國際碼查詢:http://www.iso.org/iso/country_codes/iso_3166_code_lists/english_country_names_and_code_elements.htm
3. 程式名稱國際化
建立一個infoplist.strings檔案
然後國際化它,get info。。
在下面生成的語言檔案裡寫上cfbundledisplayname="中文名";
然後編輯info.plist,添加一個新的屬性application has localized display name, 設定其類型為boolean,并将其value設定為選中狀态。
在程式中使用字元串的地方可以調用nslocalizedstring來取出對應語言的字元串
如果你的strings檔案不是标準的localizable.strings,假設叫my.strings 則可以用
nslocalizedstringfromtable (@"button",@"my", nil);來取得字元串