Cocoa - 来自 NSDate、NSCalendarDate 的本地化字符串 [英] Cocoa - Localized string from NSDate, NSCalendarDate
问题描述
我正在使用 NSDate 来获取诸如18 Jun 09"之类的字符串,代码:
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.
Cocoa(本例中为 Mac OS X 10.4、10.5)是否为这种本地化提供了便利,或者我是否必须为每天的每种情况手动本地化?月份命名我自己?
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.)
推荐答案
这里有一个 来自 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);
这篇关于Cocoa - 来自 NSDate、NSCalendarDate 的本地化字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!