NSLocale currentLocale 总是返回“en

编程入门 行业动态 更新时间:2024-10-26 06:30:25
本文介绍了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

本文发布于:2023-11-13 01:36:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583051.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NSLocale   currentLocale   en

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!