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

NSLocale currentLocale always returns quot;en_USquot; not user#39;s current language(NSLocale currentLocale 总是返回“en_US;不是用户当前的语言)

本文介绍了NSLocale currentLocale 总是返回“en_US";不是用户当前的语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对 iPhone 应用程序进行国际化 - 我需要根据用户当前的语言环境对某些视图进行编程更改.我要疯了,因为无论 iPhone 模拟器或实际硬件上的语言偏好是什么,locale 总是评估为en_US":

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, locale always evaluates to "en_US":

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

令人疯狂的是,应用程序的其余部分按预期运行.从 Localization.strings 文件中选择正确的字符串并在界面中使用,并使用所选语言环境的正确 .xib 文件.

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.

我也尝试了以下方法,但无济于事,结果相同:

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?

我曾经做过的事情:

正如 Darren 的回答所暗示的,我正在寻找的偏好不在 NSLocale 中,而是在这里:

As Darren's answer suggests, the preference I'm looking for is not in NSLocale, rather it is here:

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

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

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

推荐答案

不是直接使用未记录的键查询默认值,向 NSLocale class 询问首选语言数组.

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

这篇关于NSLocale currentLocale 总是返回“en_US";不是用户当前的语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:NSLocale currentLocale 总是返回“en_US";不是用户当前的语言

基础教程推荐