[objc/swift5] Device Language Code 디바이스 언어코드 가져오기

천현철·2021년 1월 21일
0

iOS

목록 보기
13/51
post-custom-banner

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

profile
기도하지말고 행동하라
post-custom-banner

0개의 댓글