随着时代的发展,应用程序相继出现了不同语言的版本方案,中文,英文,法文,韩文等等;想在应用程序中实现语言的自由切换,需要配置多个语言的文件,根据用户的动态选择获取不同语言文件下的语言文件,从而显示到界面上面。这里先说中英文的切换,至于其他语言操作复制即可!!!

1.新建工程之类的就不用说了,配置项目本地化支持简体中文和英文

      工程-PROJECT-info->Localizations,点“+”,选择(Chinese(Simplified))添加简体中文,英文Xcode自带有(English),所以不需要再次添加。(点击添加弹出语言菜单,即可选择你所需要的语言)

app内切换语言更新界面 iOS app语言更改_Languages

2.现在可以添加多语言文件了。

   注意:需要手动切换语言,而不是根据系统语言变换,所以命名不能与系统语言文件同名,即Localizable.strings;

自己随意命名,后缀是.strings,一定要记得Targets要勾选项目

app内切换语言更新界面 iOS app语言更改_app内切换语言更新界面 iOS_02

3.配置English.strings本地化,现在支持中文和英文。

  (1)选中English.strings文件,查看右边栏中的信息;(2)点击Localization中的按钮,弹出框中显示出前面添加的语言;(3)选择中文和英文,base选项可有可无,

app内切换语言更新界面 iOS app语言更改_扫一扫_03

app内切换语言更新界面 iOS app语言更改_bundle_04

勾选之后,English.strings中多出了两个文件

app内切换语言更新界面 iOS app语言更改_bundle_05

完成之后便是完成了项目内多语言环境的配置,下面看一下这两个文件中应该怎样写

4.文件中添加需要的字符串

中文:

"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" = "扫码绑定";//扫码绑定


英文:


"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;//设置当前语言

ChangeLanguage.m

static NSBundle *bundle = nil;
+ ( NSBundle
    
return bundle;
}

//首次加载的时候先检测语言是否存在
+(void)initUserLanguage{
    
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    
NSString *currLanguage = [def valueForKey:LocalLanguageKey];
    
if(!currLanguage){
NSArray *preferredLanguages = [NSLocale preferredLanguages];
0];
if ([currLanguage hasPrefix:@"en"]) {
@"en";
else if ([currLanguage hasPrefix:@"zh"]) {
@"zh-Hans";
else currLanguage = @"en";
setValue:currLanguage forKey:LocalLanguageKey];
synchronize];
    }
    
    //获取文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:currLanguage ofType:@"lproj"];
bundle = [NSBundle bundleWithPath:path];//生成bundle
}

//获取当前语言
+(NSString
    
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    
NSString *language = [def valueForKey:LocalLanguageKey];
    
return
}
 // 
设置语言 
 
+(void)setUserlanguage:(NSString
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *currLanguage = [userDefaults valueForKey:LocalLanguageKey];
if ([currLanguage isEqualToString:language]) {
return;
    }
setValue:language forKey:LocalLanguageKey];
synchronize];
 
NSString *path = [[NSBundle mainBundle] pathForResource:language ofType:@"lproj"
bundle = [NSBundle bundleWithPath:path];
}