國際化處理時候,也要考慮NSLocale問題。
本地化封裝了關於語言,文化以及技術約定和規範的信息。用於提供於用戶所處地域相關的定製化信息和首選項信息的設置。通過獲取用戶的本地化信息設置,我們可以爲用戶提供更加友好人性化的界面設置,包括更改應用程序的界面的語言,貨幣類型,數字,日期格式的格式化,提供正確的地理位置顯示等等。iOS內置爲應用程序的開發提供了很好的本地化機制,良好的本地化意味着應用程序可以爲更多的用戶提供服務。其中NSLocale類的的主要作用便是用來封裝本地化相關的各種信息,下面簡單列舉下NSLocale的一些方法,但NSLocale更多是使用在對數字,時間日期本地化的處理的過程。
1.創建本地化對象
2 |
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@ "en_US" ]; |
5 |
[NSLocale currentLocale] |
2.獲取系統本地化信息
02 |
[NSLocale availableLocaleIdentifiers] ; |
05 |
[NSLocale ISOCountryCodes] ; |
08 |
[NSLocale ISOCurrencyCodes] ; |
11 |
[NSLocale ISOLanguageCodes] ; |
3.獲取當前系統設置語言的標識符
1 |
[[NSLocale currentLocale] localeIdentifier]; |
3 |
[[NSLocale currentLocale] objectForKey:NSLocaleIdentifier]; |
4.獲取本地化對象的具體內容
1 |
NSLocale *local = [NSLocale currentLocale]; |
3 |
[local objectForKey:NSLocaleIdentifier]; |
5 |
[local objectForKey: NSLocaleLanguageCode]; |
key值參見NSLocale Calendar Keys
5.獲取當前語言的排版方向和字符方向
1 |
[NSLocale lineDirectionForLanguage:[[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]; |
3 |
[NSLocale characterDirectionForLanguage:[[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode] ; |
6.獲取用戶的語言偏好設置列表,該列表對應於IOS中Setting>General>Language彈出的面板中的語言列表。
1 |
[NSLocale preferredLanguages] |
第一個元素即爲當前用戶設置的語言
7.監聽用戶本地化設置的消息
1 |
[[NSNotificationCenter defaultCenter] addObserver:self |
2 |
selector: @selector (localChangedHandler:) |
3 |
name:NSCurrentLocaleDidChangeNotification object:nil]; |
8.以本地化方式獲取國際化信息的顯示名稱
1 |
NSLocale *curLocal = [[NSLocale alloc]initWithLocaleIdentifier:@ "zh-Hans" ] ; |
3 |
NSLog(@ "%@" ,[curLocal displayNameForKey:NSLocaleIdentifier value:@ "fr_FR" ] ); |
5 |
curLocal = [[NSLocale alloc]initWithLocaleIdentifier:@ "zh-Hant" ] ; |
7 |
NSLog(@ "%@" ,[curLocal displayNameForKey:NSLocaleIdentifier value:@ "fr_FR" ] ); |
NSLocale
若你只開發中國區的應用,需要保證用戶修改當前語言環境時應用的顯示不發生變化。而像NSDateFormatter這樣的類,會根據設備的設置,自動返回不同語言的數據。爲了保證返回數據的語言一致,我們需要設置NSLocale。
2 |
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@ "zh" ]; |
3 |
NSDateFormatter *secondDateFormatter = [[NSDateFormatter alloc] init]; |
4 |
[secondDateFormatter setDateFormat:@ "cccc" ]; |
5 |
secondDateFormatter.locale = locale; |
6 |
NSDate *date = [NSDate date]; |
7 |
NSLog(@ "%@" , [secondDateFormatter stringFromDate:date]); |
當然,像上面的需求很罕見。
作爲大家都不常用的一個類,NSLocale類是將與國家和語言相關的信息進行簡單的組合,包括貨幣,文學方面的信息。
貨幣:貨幣的國際名稱(人民幣的國際貨幣名稱是CNY);貨幣符號(人民幣的國際貨幣符號是¥)
文學:標點符號,文字的書寫順序(左右順序),引用的起止符號等等
若做金融一類的應用可能會用到NSLocale這個類。
這個類稍微瞭解即可。
不要用NSLog輸出來查看NSDate,NSDate本身存的就是UTC時間(無論你怎麼換timeZone都不會有變化),可以將NSDate轉換爲string來查看,所以你用CST換成GMT就是ok的,(格林尼治時間已經不再被作爲標準時間使用。現在的標準時間——協調世界時(UTC)——由原子鐘提供),更多信息可以查看文檔,可以用下列代碼打印查看具體的情況
for (NSString *timeZone in [NSTimeZone knownTimeZoneNames]) {
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:timeZone]];
NSDate *currentDateStr = [dateFormatter dateFromString:@"Thu Aug 04 02:47:40 CST 2016"];
NSString *dateString = [dateFormatter stringFromDate:currentDateStr];
NSLog(@"date:%@,string:%@",currentDateStr,dateString);
}
1.如題,我想將一個日期字符串轉換爲自己想要的日期格式如下:
原字符串:NSString* string = @"Thu Aug 04 02:47:40 CST 2016";目標日期格式:2016-08-04 02:47:40
代碼如下:
NSDateFormatter* dateFormatter =[[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[dateFormatter setDateFormat:@"EEE MMM dd HH:mm:ss zzz yyyy"];
NSDate *currentDateStr = [dateFormatter dateFromString:@"Thu Aug 04 02:47:40 CST 2016"];
NSLog(@"%@",currentDateStr);
輸出結果爲:2016-08-03 18:47:40 +0000