NSLocale currentLocale always returns quot;en_USquot; not user#39;s current language(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";不是用户当前的语言
基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01