ios應用在不同語言及區域的日期顯示 – iPhone手機開發技術文章 iPhone軟體開發教學課程

在不同的語言環境下,應用展現出來的日期格式都是不一樣的。例如英文的語言環境下,展現的日期January 27, 2014。而中文的語言環境下,展現的日期2014127

這是多語言環境下,任何應用必須面對的問題。

使用NSDateFormatter
可以處理好這個問題。這是apple人員在多種語言環境下打造出來的api,放心用。不必再去發明一個輪子。

對於數字的顯示,NSNumberFormatter可以處理好這個問題。這裡隻介紹日期的顯示格式。

我們要顯示年月日,可以使用固有的類型NSDateFormatterFullStyle,可以顯示符合各種語言環境下符合其語言特征的日期。

例如

法文在法國的顯示方式27 janvier 2014

英文在美國的顯示方式January 27, 2014

英文在英國的顯示方式27 January 2014

當然,在中國就是這樣:2014127

如果要加上一個星期幾呢。也是一樣的。

這是常見的顯示格式使用默認的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

發佈留言