ios - 如何使用 Swift 在 iOS 上获取 ISO 639-2 国家代码
问题描述
目前我使用了以下代码,但它只会返回两位数的代码
if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
print("Code => \(countryCode)") //FR
}
但我需要三位数代码 (FRA),请有人建议我获取 ISO
639-2 代码
我也检查了 Apple 文档,但我不知道如何获得确切的代码
谢谢你。
解决方案
在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]
}
}
推荐阅读
- apache-spark - 使用 spark_sanitize_names 管理 pyspark 数据框中具有重复名称的多个列
- linux - Linux shell script on background, loop exit reading keyboard buffer
- python - lxml html ubnable to parse display block
- solr - 调用 Alfresco Rest API 查询时出错
- python - MAPE 正在生成 inf 作为输出
- javascript - Warning: This synthetic event is reused for performance reasons React
- tensorflow - Tensor Flow 可以用于无监督学习来解决 nlp 分类问题吗?
- python - Mathplotlib 不会在 IPython 中绘图
- vue.js - Vue 格式数组作为 WA 消息发送
- c++ - 如何将实际的 sf::Sprite 对象保存在数组或向量中?C++