2017-04-12 7 views
0

나는 ["US","CZ","FR"] 등의 국가 코드 배열을 가지고 있으며이 배열을 통해 지역화 된 국가 이름을 반복하고 싶습니다.지역 코드에서 국가 이름 수신

이전에 localizedString(forRegionCode:)은 완벽하게 작동했습니다. 그러나 아마도 10.3이 바뀌었을 것입니다. 여기

은 예입니다 :

let currentLocale = Locale.current 
// en_US (current) 
let currentIdentifier = Locale.current.identifier 
// "en_US" 
let localeFromCurrentIdentifier = Locale(identifier: currentIdentifier) 
// "en_US" (fixed) 

let franceCode = "fr" 

let franceName = currentLocale.localizedString(forRegionCode: franceCode) 
// nil 
let franceName2 = localeFromCurrentIdentifier.localizedString(forRegionCode: franceCode) 
// "France" 

왜 현재 로케일 프랑스 코드와 France를 반환 현재 로케일의 식별자에서 초기화 로케일 nil을 반환?

업데이트 : 그래서 해결 방법 localizedString(forRegionCode:) 작업 Locale.current하지만 Locale(identifier: Locale.current.identifier)을 사용하지 않는 것입니다 수 있도록합니다.

+0

Xcode 8.3.1의 10.3 시뮬레이터에서 두 버전 모두 작동합니다. –

+0

놀이터 문제 일 수 있습니다. – rmaddy

+0

@ChrisAllwein 흥미로운 점은, 별도의 로케일을 초기화하는 대신'current' 만 사용하면 내 앱이 제대로 작동하지 않는다는 것입니다 :/ – njuri

답변

0

위키 피 디아에서 각 국가 코드를 기본 언어 코드 (예 : this table)로 매핑하는 테이블을 유지 관리해야합니다. 공식 언어가 여러 개있을 수도 있고 동일한 언어의 유사어 (Simplified vs Traditional Chinese)가있을 수도 있으므로 각 contry의 기본 언어를 선택하십시오.

let countryCodes = ["US","CZ","FR"] 

// Ideally, this should come from a plist 
let defaultLanguageCodes = [ 
    "US": "en", 
    "CZ": "cs", 
    "FR": "fr" 
] 

let countryNames : [String] = countryCodes.map { 
    let languageCode = defaultLanguageCodes[$0]! 
    let localeIdentifier = "\(languageCode)_\($0.lowercased())" 
    let locale = Locale(identifier: localeIdentifier) 

    return locale.localizedString(forRegionCode: $0)! 
} 

print(countryNames) // United States, Česká republika, France