如何使用 Swift 在 iOS 上获取 ISO 639-2 国家/地区代码

Nan*_*mar 5 iphone ios swift swift2 swift3

目前我使用了以下代码,但它只会返回两位数代码

    if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
      print("Code => \(countryCode)") //FR
    }
Run Code Online (Sandbox Code Playgroud)

但我需要三位数代码(FRA),请有人建议我获取 ISO

639-2 代码

我还检查了Apple文档,但我不知道如何获取确切的代码

在此输入图像描述

https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPInternational/LanguageandLocaleIDs/LanguageandLocaleIDs.html#//apple_ref/doc/uid/10000171i-CH15

谢谢。

Jay*_*eep 1

不,你不能直接获取ISO代码,Locale不直接提供代码,你可以通过其他方式提取代码

https://github.com/lukes/ISO-3166-Countries-with-Regional-Codes/blob/master/all/all.json

从上面的 URL 下载 json,它将为您提供所有国家/地区的详细信息,还包含带有 alpha-2 代码的 ISO 代码。

因此,您可以根据您的区域设置代码获取 ISO(alpha-3) 代码。