2010-09-14 28 views
0

如何輸出en_GB,需要en。AppleLanguages也給我了語言環境,如果我只想知道語言

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

它並不總是奏效。例如,zh_CN與zh_CN完全不同。 – kennytm 2010-09-14 10:13:48

回答

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

編輯:

一個更好的辦法是:

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

現在,如果當前的語言是法語(fr_FR時)這將返回:

The current language is: français (France) 

如果當前語言是英語(EN_US)這將返回

The current language is: English (United States) 

現在,如果你想ommit國名使用componentsSeperatedByString:@" ("如上所述..

+0

謝謝;)與當前的語言,我也可以選擇索引0的對象,然後我收到「恩」,而不是我以前有。 – Lewion 2010-09-14 11:09:31