可可-来自NSDate,NSCalendarDate的本地化字符串 [英] Cocoa - Localized string from NSDate, NSCalendarDate

查看:57
本文介绍了可可-来自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)是否提供了这种本地化的工具,还是我必须针对day和&一个月来命名我的自我?

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

这篇关于可可-来自NSDate,NSCalendarDate的本地化字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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