NSLocale currentLocale 总是返回“ en_US”而不是用户的当前语言

我正在国际化一个 iPhone 应用程序的过程中——我需要根据用户当前的语言环境对某些视图进行编程更改。我要发疯了,因为不管 iPhone 模拟器或实际硬件的语言偏好是什么,locale的值总是“ en _ US”:

NSString *locale = [[NSLocale currentLocale] localeIdentifier];
NSLog(@"current locale: %@", locale);

令人不可思议的是,应用程序的其余部分都按照预期的方式运行。从 Localization.string 文件中选择正确的字符串并在接口中使用,而正确的。将使用所选区域设置的 xib 文件。

我还尝试了以下方法,但没有效果,结果也是一样:

NSString *locale = [[NSLocale autoupdatingCurrentLocale] localeIdentifier];
NSLog(@"current locale: %@", locale);

我是不是漏掉了什么简单的东西? 是偏好还是重要性?

我过去常做的:

正如达伦的回答所表明的那样,我所寻找的偏好并不在 NSLocale中,而是在这里:

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

彼得的回答似乎是一个更好的解决方案:

NSArray* preferredLanguages = [NSLocale preferredLanguages];
NSLog(@"preferredLanguages: %@", preferredLanguages);
64592 次浏览

[NSLocale currentLocale] is based on the device's Region Format settings, not the language. If the region is set to United States you will get en_US regardless of which language you're using.

Instead of querying defaults directly using an undocumented key, ask the NSLocale class for the array of preferred languages.

i found that if i leave "en_US" out , but have a "en" localization that is a copy of "en_US" the simulator automagically starts respecting the language settings, but as soon as "en_US" is an option it always picks it regardless of the settings.

To get the current device language use this instead:

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

As described here: Getting current device language in iOS?

I had an issue where formatting month names came out in english on a device set to french language.

My solution was to use this:

    NSLocale *locale = [NSLocale localeWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0] ];
[self.monthNameFormatter setLocale:locale];
[self.monthNameFormatter setDateFormat:@"MMMM"];
[self.monthNameFormatter stringFromDate:myDate];

Question is too old, but this code may help to many:

The main.m should look like this:

    NSString *localecode=[[NSLocale currentLocale] localeIdentifier];
localecode=[localecode substringToIndex:2]; //en_GB -> en
NSArray *arr = [NSLocale preferredLanguages];
NSMutableArray * mutable = [arr mutableCopy];
NSString *prefered = [mutable firstObject];
if(![localecode isEqualToString:prefered]){
if(![prefered isEqualToString:@"es"] && ![prefered isEqualToString:@"en"] && ![prefered isEqualToString:@"pt"]){
int index = [mutable indexOfObject:@"en"];
[mutable replaceObjectAtIndex:0 withObject:@"en"];
[mutable replaceObjectAtIndex:index withObject:prefered];
[[NSUserDefaults standardUserDefaults] setObject:mutable forKey:@"AppleLanguages"];


}
else{
int index = [mutable indexOfObject:localecode];
[mutable replaceObjectAtIndex:0 withObject:localecode];
[mutable replaceObjectAtIndex:index withObject:prefered];
[[NSUserDefaults standardUserDefaults] setObject:mutable forKey:@"AppleLanguages"];
}
}
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

What do this? If the current language of the device es Spanish, English or Portuguese use the the application uses the localized strings, by the other hand if the current language is not one of those and is not supported by application is set to English.

for me, both

NSString *localeString = [[NSLocale currentLocale] localeIdentifier];

and

NSArray *array = [NSLocale preferredLanguages];
self.label.text = array[0];

yield the same result when you're in a simulator.

It shows the correct locale when on a real iPad. However, the simulator has it´s own iOS which by default is set to en-US. Go to the simulator iOS preferences and switch language & region to the one you want.

That does the trick.

For us the issue was that we were overriding the application language and application region in our dev scheme. Make sure that the Application Language is set to System Language in the scheme options (Edit Scheme -> Options).