Home > Enterprise >  How to localize relative Text style in SwiftUI?
How to localize relative Text style in SwiftUI?

Time:11-11

I am using relative Text format for displaying remaining time for the widget. However I am not able to localize the result. It always shows in English. I changed the language and regions settings in the device but I can not see any changes. Do you have any suggestions in order to fix this issue?

Text(date, style: .relative)
Result: 1hr 1min 

CodePudding user response:

You should localize the date itself using the calendar:

struct ContentView: View {
    let calendar: Calendar = {
        var calendar = Calendar.current
        calendar.locale = .init(identifier: "fa")
        return calendar
    }()
    
    var body: some View {
        Text(Date.now, style: .relative)
            .environment(\.calendar, calendar)
    }
}
  • Related