I am in the process of localizing/internationalizing my iPhone app and have a question. In one place in my app, I show a list of the 12 months of the year. As it is in its currently non-localized state, I simply have the months January - December hard-coded into an NSArray. I'd like to use NSCalendar to programmatically build this NSArray of months based on the user's locale. What is the best way to do this?
Asked
Active
Viewed 2,656 times
3 Answers
12
You can get the localized name of the month from the NSDateFormatter:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
for ( int i = 0; i < 12; ++i ) {
NSString *monthName = [[df monthSymbols] objectAtIndex:i];
[...]
}
[df release];

Volker Voecking
- 5,203
- 2
- 38
- 35
-
4cool! pity it doesn't work in simulator, once i though it was a wrong method... till i tried it on device... tks Volker – meronix Feb 25 '11 at 08:22
-
Thanks for mentioning that it doesnt work in simulator, I was going nuts! – Raj Pawan Gumdal Feb 15 '13 at 06:51
2
No need to do the iteration, just get the months directly:
NSDateFormatter dateFormatter = [[NSDateFormatter alloc] init];
NSLog(@"Months = %@", dateFormatter.monthSymbols);
Swift:
let dateFormatter = NSDateFormatter()
print("Months = \(dateFormatter.monthSymbols)")

José
- 3,112
- 1
- 29
- 42
2
Swift version....tried in playground
Provide the locale or use 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)
RESULT
"["januari", "februari", "mars", "april", "maj", "juni", "juli", "augusti", "september", "oktober", "november", "december"]\n"

anoop4real
- 7,598
- 4
- 53
- 56