Lewion
Lewion

Reputation: 235

AppleLanguages gives me the locale too, what if I only want to know the language

Example: en_GB is output, need en.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
NSString *currentLanguage = [languages objectAtIndex:0];
NSLog(@"%@", currentLanguage);

Upvotes: 0

Views: 2440

Answers (1)

LarsJK
LarsJK

Reputation: 2196

NSArray *arrayOfSeperatedString = [currentLanguage componentsSeparatedByString:@"_"];
NSLog(@"%@", [arrayOfSeperatedString objectAtIndex:0]);

edit:

A better way would be:

NSLocale *currentLocale = [[[NSLocale alloc] initWithLocaleIdentifier:currentLanguage] autorelease]; //Sets the what laguage the language name and country name should be written in
NSString *displayNameString = [currentLocale displayNameForKey:NSLocaleIdentifier value:currentLocale]; //Sets what language you want written..

NSLog(@"The current language is: %@", displayNameString);

Now if the current language is french (fr_FR) this returns:

The current language is: français (France)

If the current language is english (en_US) this returns

The current language is: English (United States)

Now if you want to ommit the country name use componentsSeperatedByString:@" (" as described above..

Upvotes: 1

Related Questions