如何在swift中获取设备的详细语言 [英] How to get detailed language of device in swift

查看:137
本文介绍了如何在swift中获取设备的详细语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a database on the server that contains all the languages keywords that I use inside my applications and these languages could be changed any time.

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

In the language table of database there is column for Language (id , key, value, lang).

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

In the android application I read the language from device and it returns for example en-GB or en-US.

但在iOS应用程序中,我无法获得上述语言例如,它总是只返回语言( en,es,fr )。

But in the iOS application I can't get the language like the above example , It always returns just the language (en , es, fr).

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

So I can't query to database to get the language specified keywords on the iOS application. Because the languages on the database is en-GB, en-US style.

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

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

How can I get the language with more details ?

推荐答案

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

Start with the currentLocale() and ask questions about it. For example:

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

这篇关于如何在swift中获取设备的详细语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆