NSDateFormatter 忽略 NSUserDefaults 中的 AppleLanguages
我的应用程序被翻译成多种语言,但现在我想强制语言为荷兰语。我确实通过将 standardUserDefaults 中的 AppleLanguages 键设置为荷兰语来强制语言为荷兰语。这适用于本地化字符串和 xib 文件。
但 NSDateFormatter 似乎完全忽略了这个值。它只是使用 iPhone en_US 区域设置来格式化日期,即使我自己在 NSDateFormatter 对象上设置区域设置:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *dutch = [[NSLocale alloc] initWithLocaleIdentifier:@"nl_NL"];
[dateFormatter setLocale:dutch];
[dutch release];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
dateLabel.text = [dateFormatter stringFromDate:[BabyInfo getDate]];
[dateFormatter release];
有人知道如何解决这个问题吗?
更新:所以看来所有日期内容(如 NSDateFormatter 和 NSDatePicker)都没有使用语言区域设置来确定要显示的日期类型,而是查看位置区域设置。不过,我不知道如何告诉格式化程序和选择器我希望他们用荷兰语显示日期。所以仍然欢迎任何帮助,但至少我现在明白问题是什么。
My App is translated into several languages, but for now I want to force the language to Dutch. I did force the language to Dutch by setting the AppleLanguages key in the standardUserDefaults to Dutch. This is working for localized strings and xib files.
But the NSDateFormatter seems to ignore this value completely. It's just using the iPhones en_US locale to format the date, even if I set the locale myself on the NSDateFormatter object:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *dutch = [[NSLocale alloc] initWithLocaleIdentifier:@"nl_NL"];
[dateFormatter setLocale:dutch];
[dutch release];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
dateLabel.text = [dateFormatter stringFromDate:[BabyInfo getDate]];
[dateFormatter release];
Does anyone have a clue how to solve this?
Update: So it seems the all the date stuff (like NSDateFormatter and NSDatePicker) are not using the language locale to determine what type of date to show, but are looking at the location locale. Still, I don't know how to tell the formatter and picker that I want them to show their dates in Dutch. So any help is still welcome, but at least I now understand what the problem is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许这个答案来得太晚了,但请查看:
http:// /www.alexcurylo.com/blog/2011/02/16/tip-nsdateformatter-localization/
这将强制格式化程序/选择器使用首选语言的区域设置,而无需更改您的 iPhone/iPad 设置。
Maybe this answer comes too late but check this out:
http://www.alexcurylo.com/blog/2011/02/16/tip-nsdateformatter-localization/
This will force the Formatter/Picker to use the locale with the preferred language without having to change your iPhone/iPad settings.
要为日期格式化程序使用所选语言,请使用:
To use the selected language for your date formatter use: