iOS: How to Get the Device Current Language Setting?
Asked Answered
G

10

25

There are some features within my application that are supposed to be based on the language settings of the device where it's running.

I want to get the actual language and not some country settings. Foe example, if the language is English, I don't care if it's US, UK, Australia, etc...

I'm familiar with the NSLocale object, but it seems to relate to the Region Format setting and not to the Language setting (see screen shot below) so when I try to retrieve the language out of it using [locale displayNameForKey:NSLocaleIdentifier value:[locale localeIdentifier] I get things like English (United States) instead of English; also, I think that what I need is the Language data and not the Region Format (am I right?).

Can anyone direct me to how to retrieve the language setting?

enter image description here

Ginger answered 26/7, 2011 at 11:40 Comment(0)
B
52

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];
Bonine answered 26/7, 2011 at 11:49 Comment(3)
what is 's' parameter at the end? Should it be langID?Wouldbe
@Flink, langID indeed. thank your for comment, I'll edit the answer. It'sweird that error remained unspotted for so long :)Bonine
Warning: in iOS 9, the return value of the NSLocale preferredLanguages changed. If before you have been getting "en" only, in iOS 9 you will get "en-US" or "en-JP", etc. Reference: happyteamlabs.com/blog/…Hemihydrate
C
5

Try this:

NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
NSArray* arrayLanguages = [userDefaults objectForKey:@"AppleLanguages"];
NSString* currentLanguage = [arrayLanguages objectAtIndex:0];
Chong answered 26/7, 2011 at 11:54 Comment(0)
Z
3

Getting language and region in Swift:

    LF.log("language", NSLocale.preferredLanguages())
    LF.log("locale", NSBundle.mainBundle().preferredLocalizations)

In my case I'm getting:

language: '(
    "zh-Hans"
)'
locale: '(
    en
)'
Zooid answered 8/5, 2015 at 2:45 Comment(1)
NSBundle.mainBundle().preferredLocalizations does not give current device language.The gives the language that you have given in CFBundleDevelopmentRegion of info.plist if CFBundleAllowMixedLocalizations is true in info.plist then first item of CFBundleLocalizations in info.plist is returnedRuberta
C
3

In Swift 4:

let currentLanguage = Locale.current.languageCode

It will give you just the language code, no country code.

Carcassonne answered 9/11, 2018 at 6:9 Comment(0)
O
2

Swift:

let language = NSBundle.mainBundle().preferredLocalizations[0] as NSString
Othello answered 21/12, 2014 at 16:5 Comment(2)
Unfortunately this is a wrong answer. If you set your system language to French and your "Language and Region" is set to the US, you'll get "en" instead of "fr", whilst the question was asking about language.Zooid
NSBundle.mainBundle().preferredLocalizations does not give current device language.The gives the language that you have given in CFBundleDevelopmentRegion of info.plist if CFBundleAllowMixedLocalizations is true in info.plist then first item of CFBundleLocalizations in info.plist is returnedRuberta
B
2

Working solution:

    let language = NSLocale.preferredLanguages()[0]
    let languageDic = NSLocale.componentsFromLocaleIdentifier(language) as NSDictionary
    //let countryCode = languageDic.objectForKey("kCFLocaleCountryCodeKey")
    let languageCode = languageDic.objectForKey("kCFLocaleLanguageCodeKey") as! String
    print(languageCode)
Bionics answered 3/1, 2017 at 7:41 Comment(0)
A
1
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0]; 
Aeolian answered 10/3, 2014 at 13:22 Comment(0)
P
1

Find the solution in XCode's helper document, it wrote:

Getting the Current Language

To get the language that the app is using from the main application bundle, use the preferredLocalizations method in the NSBundle class:

NSString *languageID = [[NSBundle mainBundle] preferredLocalizations].firstObject;

Purloin answered 5/9, 2016 at 9:35 Comment(0)
P
0

Use below code to fetch Localised language without having trouble to the en-india, en-us etc..

 NSString *Ph = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];

In and After ios9 this code need to take in cosideration

Platyhelminth answered 18/12, 2015 at 10:33 Comment(1)
This does not give current device language.The gives the language that you have given in CFBundleDevelopmentRegion of info.plist if CFBundleAllowMixedLocalizations is true in info.plist then first item of CFBundleLocalizations in info.plist is returnedRuberta
K
-1

To know the current language selected within your localizations use

[[NSBundle mainBundle] preferredLocalizations]

Example:

NSString *language = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];

To get two letter word

NSString *language = [[[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0] substringToIndex:2];

Swift:

let language = NSBundle.mainBundle().preferredLocalizations.first as NSString
Khoury answered 28/9, 2015 at 13:39 Comment(1)
This does not give current device language.The gives the language that you have given in CFBundleDevelopmentRegion of info.plist if CFBundleAllowMixedLocalizations is true in info.plist then first item of CFBundleLocalizations in info.plist is returnedRuberta

© 2022 - 2024 — McMap. All rights reserved.