AppleLanguages也为我提供了语言环境,如果我只想知道语言的话

时间:2010-09-14 10:12:05

标签: iphone localization

示例:输出en_GB,需要en。

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

1 个答案:

答案 0 :(得分: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)

现在,如果您想省略国家/地区名称,请使用componentsSeperatedByString:@" (",如上所述..