多语言
說明:依系統設定的語言自動切換適合的顯示字串
操作:
在Interface Builder設計時的介面
若系統設定為English則顯示英文介面
若系統設定為繁體中文則顯示為中文介面
如何建立自定的語系檔:
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 <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);
}
複製代碼