NSLocale currentLocale总是返回“en_US”不是用户当前的语言 [英] NSLocale currentLocale always returns "en_US" not user's current language

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

问题描述

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

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

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



我也试过以下,无效和具有相同的结果:

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

有没有简单的东西我缺少?


$ b

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

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

彼得的回答似乎是更好的解决方案: b
$ b

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


解决方案

而不是直接使用未记录的键查询默认值, a href =http://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSLocale_Class/Reference/Reference.html#//apple_ref/occ/clm/NSLocale/preferredLanguages = nofollow noreferrer>向NSLocale 请求首选语言数组。


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);

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?

What I used to do:

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);

Peter's answer seems to be a better solution:

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

解决方案

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

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆