Getting current device language in iOS?
I'd like to show the current language that the device UI is using. What code would I use?
I want this as an NSString
in fully spelled out format. (Not @"en_US")
EDIT: For those driving on by, there are a ton of useful comments here, as the answer has evolved with new iOS releases.
Solution 1:
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
Solution 2:
The selected answer returns the current device language, but not the actual language used in the app. If you don't provide a localization in your app for the user's preferred language, the first localization available, ordered by the user's preferred order, is used.
To discover the current language selected within your localizations use
[[NSBundle mainBundle] preferredLocalizations];
Example:
NSString *language = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];
Swift:
let language = NSBundle.mainBundle().preferredLocalizations.first as NSString
Solution 3:
Solution for iOS 9:
NSString *language = [[NSLocale preferredLanguages] objectAtIndex:0];
language = "en-US"
NSDictionary *languageDic = [NSLocale componentsFromLocaleIdentifier:language];
languageDic will have the needed components
NSString *countryCode = [languageDic objectForKey:@"kCFLocaleCountryCodeKey"];
countryCode = "US"
NSString *languageCode = [languageDic objectForKey:@"kCFLocaleLanguageCodeKey"];
languageCode = "en"
Solution 4:
This will probably give you what you want:
NSLocale *locale = [NSLocale currentLocale];
NSString *language = [locale displayNameForKey:NSLocaleIdentifier
value:[locale localeIdentifier]];
It will show the name of the language, in the language itself. For example:
Français (France)
English (United States)