8

ローカライズされた日付 (曜日と月を文字で表示) を表示しようとしていますが、電話の言語 (地域の形式ではありません) によって異なります。

私は試した :

// 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]]]; 

しかし、電話の言語を変更しても何も変わらず、フランス語で表示されます。どうすればそれを機能させることができますか?

4

3 に答える 3

9

これを試してください。

最初にデバイスの言語を取得し、次に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);
于 2013-09-13T10:42:13.930 に答える