gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 00:39:00 27 4
gpt4 key购买 nike

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

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

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

639-2代码

我也查了苹果文档,但我不知道如何得到确切的代码

enter image description here

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

谢谢。

最佳答案

https://github.com/almerlucke/NSLocale-ISO639_2 提供了一个 Objective C 解决方案.

使用 https://github.com/almerlucke/NSLocale-ISO639_2/blob/master/Resources/iso639_2.bundle/iso639_1_to_iso639_2.plist 创建您自己的负载并不难

public extension Locale {

private static let allIso639_2LanguageIdentifiers: [String: String] = {
guard let path = Bundle.main.path(forResource: "iso639_1_to_iso639_2", ofType: "plist") else { return [:] }
guard let result = NSDictionary(contentsOfFile: path) as? [String: String] else { return [:] }

return result
}()

public var iso639_2LanguageCode: String? {
guard let languageCode = languageCode else { return nil }
return Locale.allIso639_2LanguageIdentifiers[languageCode]
}

}

关于ios - 如何使用 Swift 在 iOS 上获取 ISO 639-2 国家代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51536454/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com