Ohad Regev
Ohad Regev

Reputation: 5711

iOS: How to Get the Device Current Language Setting?

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

Upvotes: 25

Views: 38066

Answers (10)

Rubaiyat Jahan Mumu
Rubaiyat Jahan Mumu

Reputation: 4127

In Swift 4:

let currentLanguage = Locale.current.languageCode

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

Upvotes: 3

iDevAmit
iDevAmit

Reputation: 1578

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)

Upvotes: 2

liwp_Stephen
liwp_Stephen

Reputation: 2870

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;

Upvotes: 1

Esha
Esha

Reputation: 1303

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

Upvotes: 0

Azhar
Azhar

Reputation: 20670

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

Upvotes: -1

superarts.org
superarts.org

Reputation: 7238

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
)'

Upvotes: 3

Esqarrouth
Esqarrouth

Reputation: 39181

Swift:

let language = NSBundle.mainBundle().preferredLocalizations[0] as NSString

Upvotes: 2

Mohit
Mohit

Reputation: 3416

NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0]; 

Upvotes: 1

Vladimir
Vladimir

Reputation: 170839

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];

Upvotes: 52

Sergio
Sergio

Reputation: 1732

Try this:

NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
NSArray* arrayLanguages = [userDefaults objectForKey:@"AppleLanguages"];
NSString* currentLanguage = [arrayLanguages objectAtIndex:0];

Upvotes: 5

Related Questions