代码片段
NSArray *arLanguages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
NSLog(@"arLanguages:%@",arLanguages);
///获取设备当前地区的代码和APP语言环境
NSString *languageCode = [NSLocale preferredLanguages][0];
// 获取国际通用国家地区代码(应该和手机本身有关)
NSString *countryCode = [NSString stringWithFormat:@"-%@", [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode]];
// if (languageCode) {
// languageCode = [languageCode stringByReplacingOccurrencesOfString:countryCode withString:@""];
// }
NSLog(@"countryCode:%@ languageCode : %@",countryCode, languageCode);
///当前APP使用的语言
NSString *preferredLanguage = [[[NSBundle mainBundle] preferredLocalizations] firstObject];
//获取设备当前地区的代码和APP语言环境
NSString *localeIdentifier = [[NSLocale currentLocale] objectForKey:NSLocaleIdentifier];
NSLog(@"preferredLanguage:%@ localeIdentifier : %@",preferredLanguage, localeIdentifier);
如何设置当前APP的首选语言
- (设置->找到APP->首选语言 可以支持设置APP的首选语言)
当前的手机语言是中文简体 APP首选语言设置的也是简体中文
2022-04-27 11:03:10.822022+0800 测试域名[66765:6369052] arLanguages:(
"zh-Hans-CN",
"zh-Hant-CN",
"en-GB",
"en-CN",
"zh-Hant-HK",
"vi-CN"
)
2022-04-27 11:03:10.822107+0800 测试域名[66765:6369052] countryCode:-CN languageCode : zh-Hans-CN
2022-04-27 11:03:10.822147+0800 测试域名[66765:6369052] preferredLanguage:zh-Hans localeIdentifier : zh_CN
当前的手机语言是中文简体 APP首选语言是英文
2022-04-27 11:25:22.039326+0800 测试域名[67115:6381235] arLanguages:(
"en-GB",
"zh-Hans-CN",
"zh-Hant-CN",
"en-CN",
"zh-Hant-HK",
"vi-CN"
)
2022-04-27 11:25:22.039398+0800 测试域名[67115:6381235] countryCode:-CN languageCode : en-GB
2022-04-27 11:25:22.039447+0800 测试域名[67115:6381235] preferredLanguage:en localeIdentifier : en_CN
总结
- 获取APP的首选语言最好通过
NSString *preferredLanguage = [[[NSBundle mainBundle] preferredLocalizations] firstObject];
来获取,那样就不用截取了
- 最终选用哪种方式,最好还是要根据实际情况来使用