Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3: How to get language code from a country code?

I want to get the main language of a country. For example: "US" -> "en" or "VN" -> "vi" In Swift 3, are there any ways to do this?

like image 917
Danh Huynh Avatar asked Mar 10 '23 17:03

Danh Huynh


1 Answers

The system has a list of common locales that include both a language and region. You can use this to get common languages for a region. There is no concept of a main language in a Locale.

func commonLanguages(for region:String) -> [String] {
    return Locale.availableIdentifiers
        .map { Locale(identifier:$0) }
        .filter { $0.regionCode == region }
        .map { $0.languageCode ?? "??" }
}
like image 189
drawnonward Avatar answered Mar 15 '23 16:03

drawnonward