如何获取本地化日期单位?

时间:2012-03-28 12:04:28

标签: objective-c nsdate

我需要一个包含日期单位的数组,例如年,月,日.... 我希望它们能够被本地化,例如在德国,它将是Jahr,Monat,Tag ......

似乎我只能从日期获取本地化字符串,而不是单位。 有没有办法直接从当前日历中获取这些单位而不是手工翻译它们?

2 个答案:

答案 0 :(得分:1)

据我所知,您需要为这些单位提供自己的翻译。您可以使用NSDateFormatter获取日期和月份的名称,但不能获取单位本身的名称。如何翻译单位取决于语法案例和数字,因此翻译是上下文敏感的。别忘了,有些语言只有grammatical numbers而不仅仅是单数和复数!

答案 1 :(得分:0)

   NSCalendar *gregorian=[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
    [gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"CDT"]];
    NSDateComponents *timeZoneComps=[[NSDateComponents alloc] init];
    [timeZoneComps setHour:16];
    //specify whatever day, month, and year is appropriate
    NSDate *date=[gregorian dateFromComponents:timeZoneComps];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
    NSString *monthName = [[df monthSymbols] objectAtIndex:(mymonth-1)];
    [df setDateFormat:@"EEE"];
   NSString *weekName = [ df stringFromDate:date];
    NSLog(@"The Week Name :: %@",weekName);
    [df release];

您可以使用上面的代码,特别是timeZoneWithAbbreviation:@"CDT"代表各种时区的缩写,如CDT,GMT,UTC,CST。我认为这会对你有所帮助。

相关问题