我正在尝试显示本地化日期(以字母表示的周日和月份)取决于手机的语言(而不是区域格式).
我试过了 :
// 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]]];
但是当我改变手机的语言时,它什么都没改变,它仍然以法语显示.我怎样才能使它工作?
解决方法
试试这个;
首先我们获取设备语言,然后设置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);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。