objective-c NSLocale currentLocale 总是返回“en_US”而不是用户的当前语言
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1522210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSLocale currentLocale always returns "en_US" not user's current language
提问by prairiedogg
I'm in the processes of internationalizing an iPhone app - I need to make programmatic changes to certain views based on what the user's current locale is. I'm going nuts because no matter what the language preference on the iPhone simulator or actual hardware are, localealways evaluates to "en_US":
我正在对 iPhone 应用程序进行国际化 - 我需要根据用户当前的语言环境对某些视图进行编程更改。我要疯了,因为无论 iPhone 模拟器或实际硬件上的语言偏好是什么,locale总是评估为“en_US”:
NSString *locale = [[NSLocale currentLocale] localeIdentifier];
NSLog(@"current locale: %@", locale);
The crazy thing is that the rest of the application behaves as expected. The correct strings are selected from the Localization.strings file and used in the interface, and the correct .xib files for the selected locale are used.
疯狂的是,应用程序的其余部分按预期运行。从 Localization.strings 文件中选择正确的字符串并在界面中使用,并使用所选语言环境的正确 .xib 文件。
I have also tried the following, to no avail and with the same result:
我也尝试了以下方法,但无济于事,结果相同:
NSString *locale = [[NSLocale autoupdatingCurrentLocale] localeIdentifier];
NSLog(@"current locale: %@", locale);
Is there something simple I'm missing? A preference or an import perhaps?
我错过了一些简单的东西吗?可能是偏好还是进口?
What I used to do:
我曾经做过的事情:
As Darren's answer suggests, the preference I'm looking for is not in NSLocale, rather it is here:
正如达伦的回答所暗示的那样,我正在寻找的偏好不在NSLocale,而是在这里:
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
NSArray* languages = [userDefaults objectForKey:@"AppleLanguages"];
NSString* preferredLanguage = [languages objectAtIndex:0];
NSLog(@"preferredLanguage: %@", preferredLang);
Peter's answer seems to be a better solution:
彼得的回答似乎是一个更好的解决方案:
NSArray* preferredLanguages = [NSLocale preferredLanguages];
NSLog(@"preferredLanguages: %@", preferredLanguages);
采纳答案by Peter Hosey
Instead of querying defaults directly using an undocumented key, ask the NSLocale classfor the array of preferred languages.
不要直接使用未记录的键查询默认值,而是向 NSLocale类询问首选语言的数组。
回答by Darren
[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.
[NSLocale currentLocale]基于设备的区域格式设置,而不是语言。如果区域设置为美国,无论您使用哪种语言,您都将获得 en_US。
回答by sust86
To get the current device language use this instead:
要获取当前设备语言,请使用以下命令:
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
As described here: Getting current device language in iOS?
如此处所述: 在 iOS 中获取当前设备语言?
回答by Simon Bengtsson
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).
对我们来说,问题是我们在我们的开发方案中覆盖了应用程序语言和应用程序区域。确保在方案选项(编辑方案 -> 选项)中将应用程序语言设置为系统语言。
回答by AlexW
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.
当您在模拟器中时产生相同的结果。
回答by Ben Clayton
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];
回答by Pants
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.
我发现如果我不考虑“en_US”,但有一个“en”本地化,它是“en_US”的副本,模拟器会自动开始尊重语言设置,但是只要“en_US”是一个选项,它总是会选择它,不管的设置。
回答by cdiazmo
Question is too old, but this code may help to many:
问题太老了,但这段代码可能对很多人有帮助:
The main.m should look like this:
main.m 应该是这样的:
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.
这是做什么?如果设备的当前语言是西班牙语、英语或葡萄牙语,则应用程序使用本地化字符串,另一方面,如果当前语言不是其中之一且应用程序不支持,则将其设置为英语。
回答by Ronald Hofmann
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.
它在真正的 iPad 上显示正确的语言环境。但是,模拟器有自己的 iOS,默认设置为 en-US。转到模拟器 iOS 首选项并将语言和区域切换到您想要的语言和区域。
That does the trick.
这就是诀窍。

