How to get detailed device language in fast

I have a database on the server that contains all the keywords that I use in my applications, and these languages ​​can be changed at any time.

The database language table has a column for Language (id , key, value, lang) .

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

But in the iOS app, I cannot get the language, as in the above example. It always returns only the language ( en , es, fr ).

Therefore, I cannot query the database to retrieve the specified keywords in the iOS app. Because the languages ​​in the database are en-GB, en-US style.

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

How can I get a language with more details?

+8
ios objective-c swift
source share
5 answers

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

 let lang = NSLocale.currentLocale().localeIdentifier 

Or at a finer level of detail:

 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 
+14
source share

Swift 3 does not have an NSLocaleCountryCode , it is replaced with a regionCode

https://developer.apple.com/reference/foundation/nslocale.key/1417845-countrycode

decision:

 let langCode = Locale.current.languageCode let regionCode = Locale.current.regionCode let language = "\(langCode)-\(regionCode)" 
+10
source share

For Swift 4, use instead:

 NSLocale.current.identifier //Output: en-US, pt-BR 
+1
source share

just use this line of code to solve:

let identifier = Locale.current.identifier

0
source share

No extra effort, just use the following to suit your requirements.

 Locale.current.identifier //en_US Locale.current.collatorIdentifier //en-US 
0
source share

All Articles