Cocoa - Localized string from NSDate, NSCalendarDate

我只是一个虾纸丫 提交于 2019-11-29 01:57:27

问题


I'm using NSDate to get a string such as "18 Jun 09", with code:

NSDate *theDate = [NSDate date];
NSString *dateString = [theDate descriptionWithCalendarFormat:@"%d %b %y"
        timeZone:nil
        locale: nil];

This works, but only results in an English output. I need the output to be localized in the user's default language.

Does Cocoa (Mac OS X 10.4, 10.5 in this case) provide a facility for this localization or do I have to manually localize for each case of day and & month names my self?

(I have provided a locale, but although that does provide a locale-specific ordering of the date, it does not appear to do any localization of day-month names.)


回答1:


Here is a snippet from Apple:

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];

[dateFormatter setDateStyle:NSDateFormatterMediumStyle];

[dateFormatter setTimeStyle:NSDateFormatterNoStyle];

NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:118800];

NSString *formattedDateString = [dateFormatter stringFromDate:date];

NSLog(@"formattedDateString for locale %@: %@", [[dateFormatter locale] localeIdentifier], formattedDateString);



回答2:


It appears one line of code is missing:

[dateFormatter setLocale:usLocale];


来源:https://stackoverflow.com/questions/1011175/cocoa-localized-string-from-nsdate-nscalendardate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!