SwiftUI Widget Locale doesn't apply

Viewed 321

I'm trying to localize the date in my widget, I want to use the Italian locale "it" that I already used elsewhere in my main application with this exact code but somehow, on my widget both on the simulator and on real devices the locale doesn't change

This is my date formatter in my ViewModel

static let dateFormat: DateFormatter = {
    let formatter = DateFormatter()
    formatter.dateFormat = "EEEE dd"
    formatter.locale = Locale(identifier: "it")
    return formatter
}()

and this is how I format the date in my View

Text("\(viewModel.nextDay, formatter: ViewModel.dateFormat)")
1 Answers

It is possible to use SwiftUI locale environment key, like

demo

Text(entry.date, style: .date)
    .bold()
    .environment(\.locale, Locale(identifier: "it"))

Tested with Xcode 12.1

Related