How to get system device language, swift iOS
Try this
UserDefaults.standard.stringArray(forKey: "AppleLanguages")
The output will be like - ["en-US"]
.
It'll return an array with language codes, first index is the current language set in iPhone's setting (it'll be a single item array if the preferred language order in settings is empty.)
How to get detailed language of device in swift
Start with the currentLocale()
and ask questions about it. For example:
let lang = NSLocale.currentLocale().localeIdentifier
Or, at a finer level of granularity:
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
Getting current device language in iOS?
The solutions provided will actually return the current region of the device - not the currently selected language. These are often one and the same. However, if I am in North America and I set my language to Japanese, my region will still be English (United States). In order to retrieve the currently selected language, you can do:
NSString * language = [[NSLocale preferredLanguages] firstObject];
This will return a two letter code for the currently selected language. "en" for English, "es" for Spanish, "de" for German, etc. For more examples, please see this Wikipedia entry (in particular, the 639-1 column):
List of ISO 639-1 codes
Then it's a simple matter of converting the two letter codes to the string you would like to display. So if it's "en", display "English".
EDIT
Worth to quote the header information from NSLocale.h:
+ (NSArray *)preferredLanguages NS_AVAILABLE(10_5, 2_0); // note that this list does not indicate what language the app is actually running in; the [NSBundle mainBundle] object determines that at launch and knows that information
People interested in app language take a look at @mindvision's answer
iOS:How to get current application language
What i always do:
Add a string entry into the Localizable.strings files.
I always use the key "lang"="de";
(or "lang"="en"
, etc.).
Then you can use it in your NSURLRequest by adding the language over NSLocalizedString(@"lang", @"")
With that method you have absolute control what is going to be sent to you backend.
Getting current device language in iOS?
The solutions provided will actually return the current region of the device - not the currently selected language. These are often one and the same. However, if I am in North America and I set my language to Japanese, my region will still be English (United States). In order to retrieve the currently selected language, you can do:
NSString * language = [[NSLocale preferredLanguages] firstObject];
This will return a two letter code for the currently selected language. "en" for English, "es" for Spanish, "de" for German, etc. For more examples, please see this Wikipedia entry (in particular, the 639-1 column):
List of ISO 639-1 codes
Then it's a simple matter of converting the two letter codes to the string you would like to display. So if it's "en", display "English".
EDIT
Worth to quote the header information from NSLocale.h:
+ (NSArray *)preferredLanguages NS_AVAILABLE(10_5, 2_0); // note that this list does not indicate what language the app is actually running in; the [NSBundle mainBundle] object determines that at launch and knows that information
People interested in app language take a look at @mindvision's answer
Related Topics
Keep a UIview or UIviewcontroller on Top of All Others
Sprite Kit Collision Detection
Sorting Nsarray Containing Nsdate Objects
Play Keyboard Click Sound in a Collection View Controller
How to Disable The Display of The Notification Center When Pulling Down on The Screen
Remove Duplicate Structs in Array Based on Struct Property in Swift
How to Install Self-Signed Certificates in iOS 11
Xcode Debug View Hierarchy: Unable to Capture View Hierarchy
How to Use Indexesofobjectspassingtest: in Swift
Force UIsplitviewcontroller to Always Show Master (Only) in Landscape (On iPhone 6 Plus)
Url Opening Swift App - Open Works - Called Function Does Not Work
Swift 3 Weird Crashes (Type Inference)
How to Display Collectionviewcell in Center in Swift 4
Restkit 0.20 JSON Mapping Along with Additional Offline Data
Camera Only Takes Pictures in Portrait Mode Swift
Dynamic Height for a Uitableview Based on a Dynamic Collection View