Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDate month and week day localized

I'm trying to display a localized date (with the week day and month in letters) depends on the language of the phone (not the region format).

I tried :

// Date formatter
NSDateFormatter* df = [[NSDateFormatter alloc] init];
NSLocale* locale = [NSLocale currentLocale];
NSLog(@"locale : %@", locale.localeIdentifier);
[df setDateFormat:[NSDateFormatter dateFormatFromTemplate:@"EEEE dd MMMM" options:0 locale:[NSLocale currentLocale]]]; 

But when I change the language of the phone, it changes nothing, it still displays in french. How can I make it work ?

like image 292
Seb Avatar asked Sep 13 '13 09:09

Seb


2 Answers

Try this one;

First we get the device language, then we set the locale of NSDateFormatter with that language.

NSString * deviceLanguage = [[NSLocale preferredLanguages] objectAtIndex:0];
NSDateFormatter * dateFormatter = [NSDateFormatter new];
NSLocale * locale = [[NSLocale alloc] initWithLocaleIdentifier:deviceLanguage];

[dateFormatter setDateFormat:@"EEEE dd MMMM"];
[dateFormatter setLocale:locale];

NSString * dateString = [dateFormatter stringFromDate:[NSDate date]];

NSLog(@"%@", dateString);
like image 151
Desdenova Avatar answered Sep 30 '22 01:09

Desdenova


You are testing it wrong. What you should do is change the Region Format, not the Language of the device.

Go to Settings->General->International->Region Format to change it and test it again.

I hope it helps.

EDIT: Using the Language is not a good idea. iOS is not translated to that many languages. However it supports a lot of region formats.

Example: I live in Bulgaria. iOS is not translated to bulgarian, so the actual menus and apps use English language, but as I have set my Region Format to Bulgaria, all of the date formats, date labels and currency labels are the bulgarian ones.

like image 27
Nikola Kirev Avatar answered Sep 30 '22 01:09

Nikola Kirev