2011-02-25 50 views
5

我正在本地化/國際化我的iPhone應用程序,並有一個問題。在我的應用程序中的一個地方,我列出了今年的12個月的列表。因爲它處於目前非本地化的狀態,所以我只需將1月至12月的數月硬編碼爲NSArray。我想使用NSCalendar以編程方式基於用戶的區域設置構建這個NSArray的月份。做這個的最好方式是什麼?如何以編程方式構建本地化日曆月份名稱的NSArray?

回答

11

你可以從NSDateFormatter月份的本地化名稱:

NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
for (int i = 0; i < 12; ++i) { 
    NSString *monthName = [[df monthSymbols] objectAtIndex:i]; 
    [...] 
} 
[df release]; 
+4

爽!可惜它不能在模擬器中工作,一旦我認爲它是一種錯誤的方法...直到我在設備上嘗試它... tks Volker – meronix 2011-02-25 08:22:31

+0

感謝提到它不能在模擬器中工作,我瘋了! – 2013-02-15 06:51:57

2

不需要做迭代,剛剛獲得直接的幾個月裏:

NSDateFormatter dateFormatter = [[NSDateFormatter alloc] init]; 
NSLog(@"Months = %@", dateFormatter.monthSymbols); 

斯威夫特:

let dateFormatter = NSDateFormatter() 
print("Months = \(dateFormatter.monthSymbols)") 
0

斯威夫特版....在遊樂場試過

提供的語言環境,或者使用currentlocale

var str = "Hello, playground" 

var formatter = NSDateFormatter() 

formatter.locale = NSLocale(localeIdentifier: "sv_SE") 
//formatter.locale = NSLocale.currentLocale() // use this to find it automatically 

print(formatter.monthSymbols) 

結果

"["januari", "februari", "mars", "april", "maj", "juni", "juli", "augusti", "september", "oktober", "november", "december"]\n"