如何将星期几从英语更改为语言环境

时间:2019-12-03 08:46:00

标签: swift calendar

我正在与此吊舱https://github.com/miraan/CalendarDateRangePickerViewController一起使用日历。我更改了月份名称,但不能更改一周中的几天。

我尝试添加 components.calendar?.locale = Locale(标识符:“ ru_RU”) dateFormatter.locale = Locale.autoupdatingCurrent ,但没有任何改变。

func getWeekdayLabel(weekday: Int) -> String {

     var components = DateComponents()
     components.calendar = Calendar.current
     components.calendar?.locale = Locale(identifier: "ru_RU")
     components.weekday = weekday
     let date = Calendar.current.nextDate(after: Date(), matching: components, matchingPolicy: Calendar.MatchingPolicy.strict)
     if date == nil {
         return "E"
     }
     let dateFormatter = DateFormatter()
     dateFormatter.dateFormat = "EEEEE"
     return dateFormatter.string(from: date!)
}

enter image description here

1 个答案:

答案 0 :(得分:1)

您的代码不必要地过于复杂。

您只想得到weekday symbols。尽可能短的形式。

Calendar为此具有专用属性:veryShortStandaloneWeekdaySymbols

结果函数为:

func getWeekdayLabel(weekday: Int) -> String {
    // TODO: Move this away, and reuse, it would be the same for every call ↓
    var calendar = Calendar.autoupdatingCurrent
    calendar.locale = Locale(identifier: "ru_RU") // Or any other locale, if you wan current, just drop this line
    // TODO: ↑
    return calendar.veryShortStandaloneWeekdaySymbols[weekday - 1] // because CalendarDateRangePickerViewController uses 1...7 weekday range, and here it's 0...6(or 0..<7) 
}

如果您想更详细地描述weekdays,请使用*weekdaySymbols部分中的其他See Also

相关问题