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
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 can I get the current device language on iOS/Android?
To get the language programmatically, on Android you can use:
Locale.Default.GetDisplayLanguage(Locale.Default)
On iOS :
var userLang = NSLocale.CurrentLocale.LocaleIdentifier;
How do I get current application locale?
The selected answer returns the current device language, but not the actual language used in the app. If you don't provide a localization for the preferred language in your app:
NSString *language = NSBundle.mainBundle.preferredLocalizations.firstObject;
NSLocale *locale = NSLocale.currentLocale;
NSString *countryCode = [locale objectForKey:NSLocaleCountryCode];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSString *country = [usLocale displayNameForKey:NSLocaleCountryCode
value:countryCode];
NSLog(@"country: %@", country);
iOS: How to Get the Device Current Language Setting?
User preferred languages are stored can be retrieved from locale as array and current language identifier is the first object in that array:
NSString *currentLanguage = [[NSLocale preferredLanguages] objectAtIndex:0];
If you want language in more readable form then use displayNameForKey:value:
method of NSLocale:
NSString *langID = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString *lang = [[NSLocale currentLocale] displayNameForKey:NSLocaleLanguageCode value:langID];
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
Related Topics
Redirect to Application If Installed, Otherwise to App Store
Swift Parse Json - the Data Couldn'T Be Read Because It Isn'T in the Correct Format
Loading/Downloading Image from Url on Swift
Split a String into an Array in Swift
Converting String to Int With Swift
How to Change the Name of an iOS App
Can You Build Dynamic Libraries For iOS and Load Them At Runtime
How to Have Stored Properties in Swift, the Same Way I Had on Objective-C
How to Access a Specific Field from Cloud Firestore Firebase in Swift
Ansible Regex_Findall Multiple Strings
Symbolicating Iphone App Crash Reports
Will iOS Launch My App into the Background If It Was Force-Quit by the User
How to Set Cornerradius For Only Top-Left and Top-Right Corner of a Uiview
How to Beta Test an Iphone App
Programmatically Retrieve Memory Usage on Iphone
Getting Error "No Such Module" Using Xcode, But the Framework Is There