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 ?
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With