objc :
+ (NSString*) getLanguageCode
{
NSString *currentLanguage = [[NSLocale preferredLanguages] firstObject];
NSString *languageCode = @"unknown";
//out: zh-Hans-KR, zh-Hant-KR
if ([currentLanguage containsString:@"zh-Hans"]) //간체
languageCode = @"zhCN";
else if ([currentLanguage containsString:@"zh-Hant"]) //번체
languageCode = @"zhTW";
else{
NSDictionary *languageDic = [NSLocale componentsFromLocaleIdentifier:currentLanguage];
languageCode = [languageDic objectForKey:@"kCFLocaleLanguageCodeKey"];
//out: ko
}
return languageCode;
}
swift :
static func getLanguageCode() -> String {
var languageCode = "unknown"
guard let currentLanguage = Locale.preferredLanguages.first else {
return "nil"
}
if currentLanguage.contains("zh-Hans") { //간체
languageCode = "zhCN"
} else if currentLanguage.contains("zh-Hant") { //번체
languageCode = "zhTW"
} else {
let dict: [String : String] = NSLocale.components(fromLocaleIdentifier: currentLanguage)
guard let code = dict["kCFLocaleLanguageCodeKey"] else {
return "nil"
}
languageCode = code
}
return languageCode
}
참조
https://stackoverflow.com/questions/3910244/getting-current-device-language-in-ios