LoginSignup
6
6

More than 5 years have passed since last update.

iOSの言語環境に合わせて月表示を取得する。

Posted at

月ごとの一覧画面を作ることがあって、文字列をこんな風に作っていたのですが、

NSString *mString = [NSString stringWithFormat:@"%d %@", month, NSLocalizedString(@"Month",@"") ];

英語のリソース作ってなかったばっかりに、"3 Month"とか訳の分からないことになったので、きちんとやることにしました。そんなメモです。

現在のiOSの言語を取得します。

NSArray *langs = [NSLocale preferredLanguages];
NSString *langID = [langs objectAtIndex:0];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:langID];

日付書式を設定します。

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:locale];

月表示の一覧を取得します。

NSArray *monthSymbols = [dateFormatter monthSymbols];

言語に合わせて、1月,2月だったり、January, Febraryだったり、、がNSArrayに入ってくるので、あとは取得するだけです。

NSString *month = [ monthSymbols objectAtIndex: 0 ];
6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6