ios - 如何快速获取设备的详细语言

标签 ios objective-c swift

我在服务器上有一个数据库,其中包含我在应用程序中使用的所有语言关键字,并且这些语言可以随时更改。

在数据库的语言表中有用于Language (id , key, value, lang)

在 android 应用程序中,我从设备读取语言并返回例如 en-GBen-US

但在 iOS 应用程序中我无法像上面的例子那样获取语言,它总是只返回语言 (en , es, fr)。

所以我无法查询数据库以获取 iOS 应用程序上指定语言的关键字。因为数据库上的语言是 en-GB,en-US 风格。

var langId: String? = NSLocale.preferredLanguages().first as? String

如何获得包含更多详细信息的语言?

最佳答案

currentLocale() 开始并提出有关它的问题。例如:

let lang = NSLocale.currentLocale().localeIdentifier

或者,在更精细的粒度级别:

let langId = NSLocale.currentLocale().objectForKey(NSLocaleLanguageCode) as! String
let countryId = NSLocale.currentLocale().objectForKey(NSLocaleCountryCode) as! String
let language = "\(langId)-\(countryId)" // en-US on my machine

关于ios - 如何快速获取设备的详细语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30141174/

相关文章:

ios - 原因 : '-[_UIFullscreenPresentationController adaptivePresentationController]:

ios - 在 Swift 的数组中使用多个部分和多个字典填充 TableView

ios - IBAction 不起作用

ios - 使用 2 个 UIButton 模拟 UISegmentedControl

objective-c - 如何在 NSMutableArray 中定位最大值和位置

ios - 调整 UIViewController 的大小而不是将其从导航堆栈中移除

ios - 无法弹出 UIImagePickerController

ios - 无法更改 UITextField 的宽度

ios - 使用委托(delegate)显示/隐藏 tableview 单元格

ios - 如何在使用 Swift 关闭 ViewController 期间将值从 ViewController B 传递到 ViewController A?