除了进行手动查找之外,这似乎是不可能的。
请注意,我不是要求本地化日期。我想要“日”、“周”、“月”、“年”的实际术语。
编辑:我正在寻找本地化的翻译。例如,如果我计算两个日期之间的 3 个月,我想向用户显示“3 个月”的本地化版本。希望有帮助。
除了进行手动查找之外,这似乎是不可能的。
请注意,我不是要求本地化日期。我想要“日”、“周”、“月”、“年”的实际术语。
编辑:我正在寻找本地化的翻译。例如,如果我计算两个日期之间的 3 个月,我想向用户显示“3 个月”的本地化版本。希望有帮助。
我们可以从中提取它DateComponentsFormatter
,这是我的日历扩展:
extension Calendar {
mutating func localizedUnitTitle(_ unit: NSCalendar.Unit, value: Int = 1, locale: Locale? = nil) -> String {
let emptyString = String()
let date = Date()
let component = getComponent(from: unit)
guard let sinceUnits = self.date(byAdding: component, value: value, to: date) else {
return emptyString
}
let formatter = DateComponentsFormatter()
if locale != nil {
self.locale = locale
}
formatter.calendar = self
formatter.allowedUnits = [unit]
formatter.unitsStyle = .full
guard let string = formatter.string(from: date, to: sinceUnits) else {
return emptyString
}
return string.replacingOccurrences(of: String(value), with: emptyString).trimmingCharacters(in: .whitespaces).capitalized
}
// swiftlint:disable:next cyclomatic_complexity
private func getComponent(from unit: NSCalendar.Unit) -> Component {
let component: Component
switch unit {
case .era:
component = .era
case .year:
component = .year
case .month:
component = .month
case .day:
component = .day
case .hour:
component = .hour
case .minute:
component = .minute
case .second:
component = .second
case .weekday:
component = .weekday
case .weekdayOrdinal:
component = .weekdayOrdinal
case .quarter:
component = .quarter
case .weekOfMonth:
component = .weekOfMonth
case .weekOfYear:
component = .weekOfYear
case .yearForWeekOfYear:
component = .yearForWeekOfYear
case .nanosecond:
component = .nanosecond
case .calendar:
component = .calendar
case .timeZone:
component = .timeZone
default:
component = .calendar
}
return component
}
}
用法:
var calendar = Calendar.current
let dayString = calendar.localizedUnitTitle(.day) //Day
let weekString = calendar.localizedUnitTitle(.weekOfMonth) //Week
let monthString = calendar.localizedUnitTitle(.month) //Month
let yearString = calendar.localizedUnitTitle(.year) //Year
let locale = Locale(identifier: "uk")
let secondString = calendar.localizedUnitTitle(.second, locale: locale) //Секунда
做这样的事情来获得语言环境中的星期几:
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale: [NSLocale currentLocale]];
NSArray * weekdays = [dateFormatter weekdaySymbols];
如果你想要实际的单词“day”、“week”、“month”,我认为你必须使用 NSLocalizedString。
没有内置功能可以为您提供英文单词“day”、“week”等的本地化翻译。
你必须使用
NSLocalizedString(@"day","");
并提供语言字符串资源。
您实际上可以使用DateComponentsFormatter.localizedString(from: to:)
. 这是文档。
这是一个方便的Calendar
扩展:
extension Calendar {
func period(unit component: Component, value: Int) -> String? {
let present = Date()
guard let future = date(byAdding: component, value: value, to: present) else { return nil }
let period = dateComponents([component], from: present, to: future)
return DateComponentsFormatter.localizedString(from: period, unitsStyle: .full)
}
}
// Usage:
Calendar.current.period(unit: .month, value: 3) // "3 months"
另外,我不久前写了这篇关于相对日期的中篇文章,看看吧!
你可以只用一行来做到这一点:
DateComponentsFormatter.localizedString(from: 3.months, unitsStyle: .full) // "3 months"