隨着時代的發展,應用程序相繼出現了不同語言的版本方案,中文,英文,法文,韓文等等;想在應用程序中實現語言的自由切換,需要配置多個語言的文件,根據用戶的動態選擇獲取不同語言文件下的語言文件,從而顯示到界面上面。這裏先說中英文的切換,至於其他語言操作複製即可!!!
1.新建工程之類的就不用說了,配置項目本地化支持簡體中文和英文
工程-PROJECT-info->Localizations,點“+”,選擇(Chinese(Simplified))添加簡體中文,英文Xcode自帶有(English),所以不需要再次添加。(點擊添加彈出語言菜單,即可選擇你所需要的語言)
2.現在可以添加多語言文件了。
注意:需要手動切換語言,而不是根據系統語言變換,所以命名不能與系統語言文件同名,即Localizable.strings;
自己隨意命名,後綴是.strings,一定要記得Targets要勾選項目
3.配置English.strings本地化,現在支持中文和英文。
(1)選中English.strings文件,查看右邊欄中的信息;(2)點擊Localization中的按鈕,彈出框中顯示出前面添加的語言;(3)選擇中文和英文,base選項可有可無,
勾選之後,English.strings中多出了兩個文件
完成之後便是完成了項目內多語言環境的配置,下面看一下這兩個文件中應該怎樣寫
4.文件中添加需要的字符串
中文:
/*
English.strings
CarCare
Created by chaoyuekeji on 2017/8/7.
Copyright © 2017年 chaoyue. All rights reserved.
*/
"AirLeak" = "漏氣";
"AppUseDes" = "APP使用說明";
"AutoBind" = "自動綁定";
"AutoBindNewDevice" = "自動綁定";
"BackToDefaultSet" = "恢復默認設置";
"BatteryAlertDown" = "電量警報下限";
"BindNewDevice" = "綁定新設備";
"BleOpen" = "藍牙開啓";
"Cancel" = "取消";
"DeviceHasBeenBD" = "設備已經被綁定";
"DeviceInstallDes" = "設備安裝說明";
"Ensure" = "確定";
"FindNewDevice" = "發現新版本";
"HighTempreture" = "溫度過高";
"Help" = "幫助";
"HighPressure" = "氣壓過高";
"Home" = "首頁";
"InputRightCode" = "請輸入正確的編號";
"InputWheelCode" = "請輸入輪胎編碼\n(由0-9,a-f,A-F組成)";
"LowBattery" = "電量低";
"LowPressure" = "氣壓低";
"ManualBindNewDevice" = "手動綁定";
"ManualBind" = "手動綁定";
"PleaseBindDevice" = "請綁定設備";
"PleaseOpenBle" = "請打開藍牙";
"PressureAlertDown" = "壓力警告下限";
"PressureAlertUpper" = "壓力警告上限";
"PressureUnint" = "壓力單位";
"Searching" = "搜索中";
"SharkAlert" = "震動報警";
"SystemSet" = "系統設置";
"TempretureAlertUpper" = "溫度警告上限";
"TempretureUnint" = "溫度單位";
"LeftUpWheel" = "左前輪胎";
"RightUpWheel" = "右前輪胎";
"LeftDownWheel" = "左後輪胎";
"RightDownWheel" = "右後輪胎";
"RotationType" = "調胎";
"ScanAndScan" = "掃一掃";//掃一掃
"AlbumLab" = "相冊";
"ChooseQRCodePic" = "請選擇二維碼圖片";
"ChooseSure" = "確認";
"ScanCodeSuccess" = "掃碼綁定成功";//掃碼綁定成功
"ScanRepeat" = "請重新掃描";//請重新掃描
"ScanBind" = "掃碼綁定";//掃碼綁定
/*
English.strings
CarCare
Created by chaoyuekeji on 2017/8/7.
Copyright © 2017年 chaoyue. All rights reserved.
*/
"AirLeak" = "Air leak";
"AppUseDes" = "app use manual";
"AutoBind" = "Auto bind";
"AutoBindNewDevice" = "Auto bind";
"BackToDefaultSet" = "Restore default setting";
"BatteryAlertDown" = "Minimum alert battery";
"BindNewDevice" = "Bind new sensor";
"BleOpen" = "Ble enabled";
"Cancel" = "Cancel";
"DeviceHasBeenBD" = "Device has been bound";
"DeviceInstallDes" = "device install instructions";
"Ensure" = "Ok";
"FindNewDevice" = "New version";
"HighTempreture" = "High temperature";
"Help" = "Help";
"HighPressure" = "High pressure";
"Home" = "Home";
"InputRightCode" = "Input correct code";
"InputWheelCode" = "Input wheel code";
"LowBattery" = "Low battery ";
"LowPressure" = "Low pressure";
"ManualBindNewDevice" = "Manual bind";
"ManualBind" = "manual bind";
"PleaseBindDevice" = "Please bind sensor";
"PleaseOpenBle" = "Enable ble";
"PressureAlertDown" = "Minimum alert pressure";
"PressureAlertUpper" = "Maximum alert pressure";
"PressureUnint" = "Pressure unit ";
"Searching" = "Searching";
"SharkAlert" = "Vibration alert";
"SystemSet" = "System setting";
"TempretureAlertUpper" = "Maximum alert temperature";
"TempretureUnint" = "Temperature unit";
"LeftUpWheel" = "Left up wheal";
"RightUpWheel" = "Right up wheal";
"LeftDownWheel" = "Left down Wheal";
"RightDownWheel" = "Right down wheal";
"RotationType" = "Tyre rotation";
"ScanAndScan" = "Scan";//掃一掃
"AlbumLab" = "Album";
"ChooseQRCodePic" = "Please choose a pic of QRCode";
"ChooseSure" = "sure";
"ScanCodeSuccess" = "Scan success";//掃碼綁定成功
"ScanRepeat" = "Scan repeat";//請重新掃描
"ScanBind" = "Scan bind";//掃碼綁定
5.配置環境已經完成,現在看看代碼怎麼寫????
(1)新創建工具類,繼承自NSObject(在這裏我命名爲ChangeLanguage)
(2)ChangeLanguage.h
+(NSBundle *)bundle;//獲取當前資源文件
+(void)initUserLanguage;//初始化語言文件
+(NSString *)userLanguage;//獲取應用當前語言
+(void)setUserlanguage:(NSString *)language;//設置當前語言
static NSBundle *bundle = nil;
+ ( NSBundle * )bundle{
return bundle;
}
//首次加載的時候先檢測語言是否存在
+(void)initUserLanguage{
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSString *currLanguage = [def valueForKey:LocalLanguageKey];
if(!currLanguage){
NSArray *preferredLanguages = [NSLocale preferredLanguages];
currLanguage = preferredLanguages[0];
if ([currLanguage hasPrefix:@"en"]) {
currLanguage = @"en";
}else if ([currLanguage hasPrefix:@"zh"]) {
currLanguage = @"zh-Hans";
}else currLanguage = @"en";
[def setValue:currLanguage forKey:LocalLanguageKey];
[def synchronize];
}
//獲取文件路徑
NSString *path = [[NSBundle mainBundle] pathForResource:currLanguage ofType:@"lproj"];
bundle = [NSBundle bundleWithPath:path];//生成bundle
}
//獲取當前語言
+(NSString *)userLanguage{
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSString *language = [def valueForKey:LocalLanguageKey];
return language;
}
//設置語言
+(void)setUserlanguage:(NSString *)language{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *currLanguage = [userDefaults valueForKey:LocalLanguageKey];
if ([currLanguage isEqualToString:language]) {
return;
}
[userDefaults setValue:language forKey:LocalLanguageKey];
[userDefaults synchronize];
NSString *path = [[NSBundle mainBundle] pathForResource:language ofType:@"lproj" ];
bundle = [NSBundle bundleWithPath:path];
}
if ([Globels shareInstane].isChangeLanguage) {
//first type
_qiyaAlertLable1.text = [[ChangeLanguage bundle] localizedStringForKey:@"LowPressure" value:@"" table:@"English"];
_dinaliangAlertLable1.text = [[ChangeLanguage bundle] localizedStringForKey:@"LowBattery" value:@"" table:@"English"];
_wenduAlertLable1.text = [[ChangeLanguage bundle] localizedStringForKey:@"HighTempreture" value:@"" table:@"English"];
_luoqiAlertLable1.text = [[ChangeLanguage bundle] localizedStringForKey:@"AirLeak" value:@"" table:@"English"];
[self.unBinDBtn1 setTitle:[CommonFunction DPLocalizedString:@"PleaseBindDevice" Comment:@"請綁定設備"] forState:UIControlStateNormal];
_qiyaAlertLable2.text = [[ChangeLanguage bundle] localizedStringForKey:@"LowPressure" value:@"" table:@"English"];
_dinaliangAlertLable2.text = [[ChangeLanguage bundle] localizedStringForKey:@"LowBattery" value:@"" table:@"English"];
_wenduAlertLable2.text = [[ChangeLanguage bundle] localizedStringForKey:@"HighTempreture" value:@"" table:@"English"];
_luoqiAlertLable2.text = [[ChangeLanguage bundle] localizedStringForKey:@"AirLeak" value:@"" table:@"English"];
[self.unBinDBtn2 setTitle:[CommonFunction DPLocalizedString:@"PleaseBindDevice" Comment:@"請綁定設備"] forState:UIControlStateNormal];
_qiyaAlertLable3.text = [[ChangeLanguage bundle] localizedStringForKey:@"LowPressure" value:@"" table:@"English"];
_dinaliangAlertLable3.text = [[ChangeLanguage bundle] localizedStringForKey:@"LowBattery" value:@"" table:@"English"];
_wenduAlertLable3.text = [[ChangeLanguage bundle] localizedStringForKey:@"HighTempreture" value:@"" table:@"English"];
_luoqiAlertLable3.text = [[ChangeLanguage bundle] localizedStringForKey:@"AirLeak" value:@"" table:@"English"];
[_luoqiAlertLable3 setText:[[ChangeLanguage bundle] localizedStringForKey:@"AirLeak" value:@"" table:@"English"]];
[self.unBinDBtn3 setTitle:[CommonFunction DPLocalizedString:@"PleaseBindDevice" Comment:@"請綁定設備"] forState:UIControlStateNormal];
_qiyaAlertLable4.text = [[ChangeLanguage bundle] localizedStringForKey:@"LowPressure" value:@"" table:@"English"];
_dinaliangAlertLable4.text = [[ChangeLanguage bundle] localizedStringForKey:@"LowBattery" value:@"" table:@"English"];
_wenduAlertLable4.text = [[ChangeLanguage bundle] localizedStringForKey:@"HighTempreture" value:@"" table:@"English"];
_luoqiAlertLable4.text = [[ChangeLanguage bundle] localizedStringForKey:@"AirLeak" value:@"" table:@"English"];
[self.unBinDBtn4 setTitle:[CommonFunction DPLocalizedString:@"PleaseBindDevice" Comment:@"請綁定設備"] forState:UIControlStateNormal];
}else{
//first type
_qiyaAlertLable1.text = [CommonFunction DPLocalizedString:@"IDS_LOW_PRESSURE" Comment:@"氣壓低"];
_dinaliangAlertLable1.text = [CommonFunction DPLocalizedString:@"IDS_LOW_BATTERY" Comment:@"電量低"];
_wenduAlertLable1.text = [CommonFunction DPLocalizedString:@"IDS_GIGH_TEMPERTURE" Comment:@"溫度高"];
_luoqiAlertLable1.text = [CommonFunction DPLocalizedString:@"IDS_AIR_LEAK" Comment:@"漏氣"];
[self.unBinDBtn1 setTitle:[CommonFunction DPLocalizedString:@"IDS_PLEASE_BIND_DEVICE" Comment:@"請綁定設備"] forState:UIControlStateNormal];
_qiyaAlertLable2.text = [CommonFunction DPLocalizedString:@"IDS_LOW_PRESSURE" Comment:@"氣壓低"];
_dinaliangAlertLable2.text = [CommonFunction DPLocalizedString:@"IDS_LOW_BATTERY" Comment:@"電量低"];
_wenduAlertLable2.text = [CommonFunction DPLocalizedString:@"IDS_GIGH_TEMPERTURE" Comment:@"溫度高"];
_luoqiAlertLable2.text = [CommonFunction DPLocalizedString:@"IDS_AIR_LEAK" Comment:@"漏氣"];
[self.unBinDBtn2 setTitle:[CommonFunction DPLocalizedString:@"IDS_PLEASE_BIND_DEVICE" Comment:@"請綁定設備"] forState:UIControlStateNormal];
_qiyaAlertLable3.text = [CommonFunction DPLocalizedString:@"IDS_LOW_PRESSURE" Comment:@"氣壓低"];
_dinaliangAlertLable3.text = [CommonFunction DPLocalizedString:@"IDS_LOW_BATTERY" Comment:@"電量低"];
_wenduAlertLable3.text = [CommonFunction DPLocalizedString:@"IDS_GIGH_TEMPERTURE" Comment:@"溫度高"];
_luoqiAlertLable3.text = [CommonFunction DPLocalizedString:@"IDS_AIR_LEAK" Comment:@"漏氣"];
[self.unBinDBtn3 setTitle:[CommonFunction DPLocalizedString:@"IDS_PLEASE_BIND_DEVICE" Comment:@"請綁定設備"] forState:UIControlStateNormal];
_qiyaAlertLable4.text = [CommonFunction DPLocalizedString:@"IDS_LOW_PRESSURE" Comment:@"氣壓低"];
_dinaliangAlertLable4.text = [CommonFunction DPLocalizedString:@"IDS_LOW_BATTERY" Comment:@"電量低"];
_wenduAlertLable4.text = [CommonFunction DPLocalizedString:@"IDS_GIGH_TEMPERTURE" Comment:@"溫度高"];
_luoqiAlertLable4.text = [CommonFunction DPLocalizedString:@"IDS_AIR_LEAK" Comment:@"漏氣"];
[self.unBinDBtn4 setTitle:[CommonFunction DPLocalizedString:@"IDS_PLEASE_BIND_DEVICE" Comment:@"請綁定設備"] forState:UIControlStateNormal];
}