Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)")
like image 910
Luca Avatar asked Nov 07 '25 08:11

Luca


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

like image 125
Asperi Avatar answered Nov 09 '25 09:11

Asperi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!