Luca
Luca

Reputation: 984

SwiftUI Widget Locale doesn't apply

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)")

Upvotes: 3

Views: 924

Answers (1)

Asperi
Asperi

Reputation: 257779

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

Upvotes: 3

Related Questions