项目中提了个需求,做国际化。但是并不是常规的随着手机系统语言的国际化。要加载特定语言,无视手机系统语言。查阅了网上众多资料,有炫技用runtime的,殊不知,滥用runtime是会挖大坑的.果断放弃,后经多次尝试得出下面答案,仅供给大家参考
配置信息
xcode版本:Version 10.2.1 (10E1001)
macos版本:10.14.6 Beta (18G59b)
WeChat50d010d2cc16c45b43d01b603855d688.png
static inline NSString *localString(NSString *string) {
NSString *stringsBundlePath = [[NSBundle bundleForClass:NSClassFromString(@"IOTUtil")]
pathForResource:@"language"
ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:stringsBundlePath] ?: [NSBundle mainBundle];
//languageIOTKey
NSInteger lanuageValue = [[NSUserDefaults standardUserDefaults] integerForKey:@"languageIOTKey"];
NSString *pathForResourceStr = @"zh-HK";
switch (lanuageValue) {
case 0:
pathForResourceStr = @"zh-HK";
break;
case 1:
pathForResourceStr = @"zh-Hans";
break;
case 2:
pathForResourceStr = @"en";
break;
default:
break;
}
//zh-Hans en zh-HK
NSString * path = [bundle pathForResource:pathForResourceStr ofType:@"lproj"];
NSBundle * languageBundle = [NSBundle bundleWithPath:path];
return [languageBundle localizedStringForKey:string value:@"" table:nil];
}
方法调用
self.infoLabel.text = localString(@"bluetooth_no_authorization_string");
文件结构
WeChat527a4d02c6db16fef5befd98166521ae.png这个关键都 贴出来了,故此就不上demo了,希望可以帮助大大家
网友评论