如何以编程方式构建本地化日历月份名称的 NSArray?

发布于 2024-10-19 04:24:03 字数 166 浏览 1 评论 0原文

我正在本地化/国际化我的 iPhone 应用程序并有一个问题。在我的应用程序中的一个位置,我显示了一年中 12 个月的列表。由于它目前处于非本地化状态,我只是将 1 月到 12 月硬编码到 NSArray 中。我想使用 NSCalendar 根据用户的区域设置以编程方式构建此 NSArray 月份。最好的方法是什么?

I am in the process of localizing/internationalizing my iPhone app and have a question. In one place in my app, I show a list of the 12 months of the year. As it is in its currently non-localized state, I simply have the months January - December hard-coded into an NSArray. I'd like to use NSCalendar to programmatically build this NSArray of months based on the user's locale. What is the best way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帅的被狗咬 2024-10-26 04:24:03

您可以从 NSDateFormatter 获取月份的本地化名称:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
for ( int i = 0; i < 12; ++i ) {
    NSString *monthName = [[df monthSymbols] objectAtIndex:i];
    [...]
}
[df release];

You can get the localized name of the month from the NSDateFormatter:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
for ( int i = 0; i < 12; ++i ) {
    NSString *monthName = [[df monthSymbols] objectAtIndex:i];
    [...]
}
[df release];
楠木可依 2024-10-26 04:24:03

Swift 版本....在游乐场尝试

提供区域设置或使用当前区域

var str = "Hello, playground"

var formatter = NSDateFormatter()

formatter.locale = NSLocale(localeIdentifier: "sv_SE") 
//formatter.locale = NSLocale.currentLocale() // use this to find it automatically

print(formatter.monthSymbols)

设置结果

"["januari", "februari", "mars", "april", "maj", "juni", "juli", "augusti", "september", "oktober", "november", "december"]\n"

Swift version....tried in playground

Provide the locale or use currentlocale

var str = "Hello, playground"

var formatter = NSDateFormatter()

formatter.locale = NSLocale(localeIdentifier: "sv_SE") 
//formatter.locale = NSLocale.currentLocale() // use this to find it automatically

print(formatter.monthSymbols)

RESULT

"["januari", "februari", "mars", "april", "maj", "juni", "juli", "augusti", "september", "oktober", "november", "december"]\n"
独木成林 2024-10-26 04:24:03

无需迭代,直接获取月份即可:

NSDateFormatter dateFormatter = [[NSDateFormatter alloc] init];
NSLog(@"Months = %@", dateFormatter.monthSymbols);

Swift:

let dateFormatter = NSDateFormatter()
print("Months = \(dateFormatter.monthSymbols)")

No need to do the iteration, just get the months directly:

NSDateFormatter dateFormatter = [[NSDateFormatter alloc] init];
NSLog(@"Months = %@", dateFormatter.monthSymbols);

Swift:

let dateFormatter = NSDateFormatter()
print("Months = \(dateFormatter.monthSymbols)")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文