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";不是用户当前的语言
基础教程推荐
- NSString intValue 不能用于检索电话号码 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iOS4 创建后台定时器 2022-01-01
