views:

43

answers:

1

Example: en_GB is output, need en.

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

Title says it all.

Thanks in advance,

Lewion

+1  A: 
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..

Larsaronen
Thanks ;) With the currentLanguages I can also select the object at index 0 and then I receive "en" instead of what I had before.
Lewion