iOS应用在多语言环境下 区域的日期显示

在不同的语言环境下,应用展现出来的日期格式都是不一样的。例如英文的语言环境下,展现的日期January 27, 2014。而中文的语言环境下,展现的日期2014127

这是多语言环境下,任何应用必须面对的问题。

使用NSDateFormatter 可以处理好这个问题。这是apple人员在多种语言环境下打造出来的api,放心用。不必再去发明一个轮子。

对于数字的显示,NSNumberFormatter可以处理好这个问题。这里只介绍日期的显示格式。

我们要显示年月日,可以使用固有的类型NSDateFormatterFullStyle,可以显示符合各种语言环境下符合其语言特征的日期。

例如

法文在法国的显示方式27 janvier 2014

英文在美国的显示方式January 27, 2014

英文在英国的显示方式27 January 2014

当然,在中国就是这样:2014年1月27日

如果要加上一个星期几呢。也是一样的。

这是常见的显示格式使用默认的style都能处理好。但是,如果只显示年月呢。

我们该如何处理?

根据不同语言的显示不同设置自定义的dataformat,中文设置为yMMMM,英文设置为MMMMy。这样下来,且不是所有的语言都是设置一遍。这种方法又陷入之前的显示年月日的老路了。

可以根据NSDateFormatter的方法生成根据不同的语言区域生成一个日期格式来,这样不同的语言就实现了符合各自特征的日期格式。

NSString *dateComponents = @"yMMMMd";

NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:usLocale];

NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init];

[dateFormatter setTimeStyle:NSDateFormatterNoStyle];

[dateFormatter setDateStyle:NSDateFormatterFullStyle];

NSDate *date=[NSDatedate];

NSLog(@"Date for default locale %@: %@",

[[dateFormatterlocale] localeIdentifier], [dateFormatterstringFromDate:date]);

NSLocale *zhLocale = [[NSLocalealloc] initWithLocaleIdentifier:@"zh_CN"];

[dateFormattersetLocale:zhLocale];

NSLog(@"Date for locale %@: %@",

[[dateFormatterlocale] localeIdentifier], [dateFormatterstringFromDate:date]);

NSLocale *usLocale = [[NSLocalealloc] initWithLocaleIdentifier:@"en_US"];

[dateFormattersetLocale:usLocale];

//

NSLog(@"Date for locale %@: %@",

[[dateFormatterlocale] localeIdentifier], [dateFormatterstringFromDate:date]);

NSLocale *gbLocale = [[NSLocalealloc] initWithLocaleIdentifier:@"en_GB"];

[dateFormattersetLocale:gbLocale];

//

NSLog(@"Date for locale %@: %@",

[[dateFormatterlocale] localeIdentifier], [dateFormatterstringFromDate:date]);

NSLocale *frLocale = [[NSLocalealloc] initWithLocaleIdentifier:@"fr_FR"];

[dateFormattersetLocale:frLocale];

NSLog(@"Date for locale %@: %@",

[[dateFormatterlocale] localeIdentifier], [dateFormatterstringFromDate:date]);

2014-01-27 16:09:15.941 TestDragCV[3223:70b] Date for default locale en_US: Monday, January 27, 2014

2014-01-27 16:09:15.942 TestDragCV[3223:70b] Date for locale zh_CN: 2014127星期一

2014-01-27 16:09:15.943 TestDragCV[3223:70b] Date for locale en_US: Monday, January 27, 2014

2014-01-27 16:09:15.944 TestDragCV[3223:70b] Date for locale en_GB: Monday, 27 January 2014

2014-01-27 16:09:15.946 TestDragCV[3223:70b] Date for locale fr_FR: lundi 27 janvier 2014

NSString *dateFormat;

NSString *dateComponents = @"yMMMMd";

NSDateFormatter *monthAndYearFormatter=[[NSDateFormatteralloc] init];

dateFormat = [NSDateFormatterdateFormatFromTemplate:dateComponents options:0 locale:usLocale];

NSLog(@"Date format for %@: %@",

[usLocale displayNameForKey:NSLocaleIdentifiervalue:[usLocale localeIdentifier]], dateFormat);

monthAndYearFormatter.dateFormat=dateFormat;

NSLog(@"localeDateFormatOfYearMonth for default locale %@: %@",

[[monthAndYearFormatterlocale] localeIdentifier], [monthAndYearFormatterstringFromDate:[NSDatedate]]);

dateFormat = [NSDateFormatterdateFormatFromTemplate:dateComponents options:0 locale:gbLocale];

NSLog(@"Date format for %@: %@",

[gbLocale displayNameForKey:NSLocaleIdentifiervalue:[gbLocale localeIdentifier]], dateFormat);

monthAndYearFormatter.dateFormat=dateFormat;

NSLog(@"localeDateFormatOfYearMonth for default locale %@: %@",

[[monthAndYearFormatterlocale] localeIdentifier], [monthAndYearFormatterstringFromDate:[NSDatedate]]);

dateFormat = [NSDateFormatterdateFormatFromTemplate:dateComponents options:0 locale:frLocale];

NSLog(@"Date format for %@: %@",

[frLocale displayNameForKey:NSLocaleIdentifiervalue:[frLocale localeIdentifier]], dateFormat);

monthAndYearFormatter.dateFormat=dateFormat;

NSLog(@"localeDateFormatOfYearMonth for default locale %@: %@",

[[monthAndYearFormatterlocale] localeIdentifier], [monthAndYearFormatterstringFromDate:[NSDatedate]]);

dateFormat = [NSDateFormatterdateFormatFromTemplate:dateComponents options:0 locale:zhLocale];

NSLog(@"Date format for %@: %@",

[zhLocale displayNameForKey:NSLocaleIdentifiervalue:[zhLocale localeIdentifier]], dateFormat);

monthAndYearFormatter.dateFormat=dateFormat;

NSLog(@"localeDateFormatOfYearMonth for default locale %@: %@",

[[monthAndYearFormatterlocale] localeIdentifier], [monthAndYearFormatterstringFromDate:[NSDatedate]]);

NSLocale *tempLocale=[NSLocalecurrentLocale];

dateFormat = [NSDateFormatterdateFormatFromTemplate:dateComponents options:0locale:[NSLocalecurrentLocale]];

NSLog(@"Date format for %@: %@",

[gbLocale displayNameForKey:NSLocaleIdentifiervalue:[tempLocale localeIdentifier]], dateFormat);

monthAndYearFormatter.dateFormat=dateFormat;

NSLog(@"localeDateFormatOfYearMonth for default locale %@: %@",

[[monthAndYearFormatterlocale] localeIdentifier], [monthAndYearFormatterstringFromDate:[NSDatedate]]);

2014-01-27 16:10:48.542 TestDragCV[3241:70b] Date format for English (United States): MMMM d, y

2014-01-27 16:10:48.544 TestDragCV[3241:70b] localeDateFormatOfYearMonth for default locale en_US: January 27, 2014

2014-01-27 16:10:48.547 TestDragCV[3241:70b] Date format for English (United Kingdom): d MMMM y

2014-01-27 16:10:48.548 TestDragCV[3241:70b] localeDateFormatOfYearMonth for default locale en_US: 27 January 2014

2014-01-27 16:10:48.551 TestDragCV[3241:70b] Date format for fran?ais (France): d MMMM y

2014-01-27 16:10:48.551 TestDragCV[3241:70b] localeDateFormatOfYearMonth for default locale en_US: 27 January 2014

2014-01-27 16:10:48.554 TestDragCV[3241:70b] Date format for中文(中国): yMd

2014-01-27 16:10:48.554 TestDragCV[3241:70b] localeDateFormatOfYearMonth for default locale en_US: 2014127

2014-01-27 16:10:48.556 TestDragCV[3241:70b] Date format for English (United States): MMMM d, y

2014-01-27 16:10:48.557 TestDragCV[3241:70b] localeDateFormatOfYearMonth for default locale en_US: January 27, 2014

你可能感兴趣的:(iOS应用在多语言环境下 区域的日期显示)