业精于勤,荒于嬉
全部博文(763)
分类: C/C++
2011-04-06 23:19:38
这种方法的好处很明显:因为iPhone OS中已经内建了多语言的支持,为什么不把你的程序也做成更多人可以使用的呢?另外,这样做还可以避免使用传统的@"中文"显示为乱码的问题(因为NSString字符串的这种创建方法只支持7Bit ASCII码)
参考链接:
http://blog.csdn.net/linkai5696/archive/2010/08/02/5783177.aspx
如何建立自定的語系檔:
1.在Resources分類下新增檔案 [右鍵/Add/New File...]
2.在範本的分類選擇Other後再選擇 Strings File
3.將存檔檔名設定為 Localizable.strings
4.在 Localizable.strings 檔案上按右鍵並選擇 Get Info
5.選擇左下方的 Make File Localizable
6.將分頁標籤切換至General
7.選擇左下方的 Add Localization
8.輸入新的語系名稱 zh_TW 後按 Add
9.您會看到目前您設定了English及zh_TW兩種語系
10.在檔案清單中您將看到 Localizable Strings (English) 及 Localizable String (zh_TW)
11.開啟後編輯語言字串就可以囉,範例如下:
Localizable.strings (English)
/*
Localizable.strings
MultiLanguageDemo
Created by babyfish0226 on 2009/3/6.
Copyright 2009 __MyCompanyName__. All rights reserved.
*/
"DemoTitle"="This is English Version";
"String1"="Apple";
"String2"="BAnana";
"String3"="Orange";
"String4"="Watermelon";
"String5"="Strawberry";
複製代碼
Localizable.strings (zh_TW)
/*
Localizable.strings
MultiLanguageDemo
Created by babyfish0226 on 2009/3/6.
Copyright 2009 __MyCompanyName__. All rights reserved.
*/
"DemoTitle"="這是繁體中文的版本";
"String1"="蘋果";
"String2"="香蕉";
"String3"="橘子";
"String4"="西瓜";
"String5"="草莓";
複製代碼
程式碼:
MultiLanguageDemoViewController.h
//
// MultiLanguageDemoViewController.h
// MultiLanguageDemo
//
// Created by babyfish0226 on 2009/3/6.
// Copyright __MyCompanyName__ 2009. All rights reserved.
//
#import
@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);
}
複製代碼
延伸閱讀:Cocoa China 如何讓您的iPhone程序支持多語言環境