【问题标题】:NSDateFormatter localizedStringFromDate:dateStyle:timeStyle does not return a correctly localized string in IOS SimulatorNSDateFormatter 本地化StringFromDate:dateStyle:timeStyle 不会在IOS 模拟器中返回正确本地化的字符串
【发布时间】:2014-01-22 01:46:29
【问题描述】:

我在我的代码中使用了以下方法来获取 NSDate 对象的本地化字符串:

todayDate = [NSDate date];    
todayDateString = [NSDateFormatter localizedStringFromDate:todayDate
                                                 dateStyle:NSDateFormatterLongStyle
                                                 timeStyle:NSDateFormatterLongStyle];

我已在 IOS 模拟器 上运行应用程序,并将语言环境设置为 English,它显示: 2014 年 1 月 4 日上午 11:10:40 GMT+1 然后我将模拟器语言更改为 意大利语 并重新启动了 AppStoryboard 中的本地化字符串正确更改为 Italian,但 NSDateFormatter localizedStringFromDate:dateStyle:timeStyle: 方法生成的字符串始终以 English 本地化。是与模拟器有关的东西还是我错过了什么?我没有开发者证书,所以无法在真机上进行测试。

【问题讨论】:

  • 可能有点晚了,但请确保您实际上是在更改模拟器上的区域设置,而不仅仅是语言设置。祝你好运!

标签: ios ios7 localization ios-simulator nsdateformatter


【解决方案1】:

我无法重现您遇到的问题,但我之前也曾为localizedStringFromDate:dateStyle:timeStyle:class 方法而苦苦挣扎。我的建议是改用实例方法,然后您可以更好地控制正在发生的事情:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
dateFormatter.dateStyle = NSDateFormatterLongStyle;
dateFormatter.timeStyle = NSDateFormatterLongStyle;

NSDate *todayDate = [NSDate date];
NSString *todayDateString = [dateFormatter stringFromDate:todayDate];

NSLog(@"Todays date localy: %@", todayDateString);

祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-23
    • 2017-12-21
    • 1970-01-01
    • 2016-07-09
    相关资源
    最近更新 更多