NSDate本地和工作日本地化

Seb*_*Seb 8 objective-c nsdate ios

我试图显示一个本地化的日期(以字母表示的周日和月份)取决于手机的语言(不是区域格式).

我试过了 :

// 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]]]; 
Run Code Online (Sandbox Code Playgroud)

但是当我改变手机的语言时,它什么都没改变,它仍然以法语显示.我怎样才能使它工作?

Des*_*ova 8

试试这个;

首先我们获取设备语言,然后NSDateFormatter 使用该语言设置语言环境.

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);
Run Code Online (Sandbox Code Playgroud)