Date not fully localized with Date.FormatStyle

Is there a way using Date.FormatStyle to make the numbers also show in Hebrew the same way DateFormatter does?

For some reason using Date.FormatStyle doesn't seem to have the ability to show the numbers locally.

Here's some sample code, you can see that using DateFormatter the locale is respected and it displays properly but using Date.FormatStyle the numbers are still regular numbers.

Update: Here's a modified version of DateFormatter. The goal is using Date.FormatStyle and getting the same result as DateFormatter


import SwiftUI

struct ContentView: View {
    let date = Date.now
    
    var body: some View {
        List{
            Text(date.formatted(formatStyle.attributed))
                .environment(\.locale, 
                              Locale(languageCode: .hebrew, script: .hebrew, languageRegion: .israel))
                .environment(\.calendar, Calendar(identifier: .hebrew))
        
            Text(date, format: formattttttt)
             .environment(\.locale, 
                           Locale(languageCode: .hebrew, script: .hebrew, languageRegion: .israel))
             .environment(\.calendar, Calendar(identifier: .hebrew))
            Text(dateFormatted(date: date))
        }
    }
 
func dateFormatted(date: Date) -> String{
        let dateFormatter = DateFormatter()
        dateFormatter.calendar = Calendar(identifier: .hebrew)
        dateFormatter.locale = Locale(identifier: "he")
        dateFormatter.dateStyle = .full
        return dateFormatter.string(from: date)
    }
    let formatStyle = Date.FormatStyle(
        date: .complete, 
        time: .omitted, 
        locale: Locale(languageCode: .hebrew, script: .hebrew, languageRegion: .israel), 
        calendar: Calendar(identifier: .hebrew)
    
    )
    let formattttttt = Date.FormatStyle(
        date: .complete, time: .omitted, locale: Locale(identifier: "he"), calendar: Calendar(identifier: .hebrew)).locale(Locale(identifier: "he"))
    
    
}

Can you post an example of what you expect the time to look like?

Also, though I haven't tried to research anything yet, it may be that time style .short is being take as a request for the time to be formatted this way. Is there a different result with other time styles?

Accepted Answer

It appears that styles use the Hebrew numbering system while templates do not:

let fullyHebrewLocale = Locale(identifier: "he-IL@numbers=hebr")
let fullyHebrewCalendar = {
    var calendar = Calendar(identifier: .hebrew)
    calendar.timeZone = TimeZone(identifier: "Asia/Jerusalem")!
    calendar.locale = fullyHebrewLocale
    return calendar
}()

let latinHebrewLocale = Locale(identifier: "he-IL@numbers=latn")
let latinHebrewCalendar = {
    var calendar = Calendar(identifier: .hebrew)
    calendar.timeZone = TimeZone(identifier: "Asia/Jerusalem")!
    calendar.locale = latinHebrewLocale
    return calendar
}()

struct ContentView: View {
    let date = Date.now
    
    var body: some View {
        List{
            Text("\u{2067}FSH:\t\u{2069}" + date.formatted(formatStyleH.attributed))
                .environment(\.layoutDirection, .rightToLeft)
            Text("\u{2067}DFHS:\t\u{2069}" + dateFormattedHS(date: date))
                .environment(\.layoutDirection, .rightToLeft)
            Text("\u{2067}DFHT:\t\u{2069}" + dateFormattedHT(date: date))
                .environment(\.layoutDirection, .rightToLeft)
            Text("\u{2067}FSL:\t\t\u{2069}" + date.formatted(formatStyleL.attributed))
                .environment(\.layoutDirection, .rightToLeft)
            Text("\u{2067}DFLS:\t\u{2069}" + dateFormattedLS(date: date))
                .environment(\.layoutDirection, .rightToLeft)
            Text("\u{2067}DFLT:\t\u{2069}" + dateFormattedLT(date: date))
                .environment(\.layoutDirection, .rightToLeft)
        }
        .environment(\.layoutDirection, .rightToLeft)
    }
 
    func dateFormattedHS(date: Date) -> String{
        let dateFormatter = DateFormatter()
        dateFormatter.calendar = fullyHebrewCalendar
        dateFormatter.locale = fullyHebrewLocale
        dateFormatter.dateStyle = .full
        dateFormatter.timeStyle = .none
        return dateFormatter.string(from: date)
    }

    func dateFormattedHT(date: Date) -> String{
        let dateFormatter = DateFormatter()
        dateFormatter.calendar = fullyHebrewCalendar
        dateFormatter.locale = fullyHebrewLocale
        dateFormatter.setLocalizedDateFormatFromTemplate("yMMMMEEEEd")
        return dateFormatter.string(from: date)
    }

    let formatStyleH = Date.FormatStyle(
        date: .complete,
        time: .omitted,
        locale: fullyHebrewLocale,
        calendar: fullyHebrewCalendar
    )

    func dateFormattedLS(date: Date) -> String{
        let dateFormatter = DateFormatter()
        dateFormatter.calendar = latinHebrewCalendar
        dateFormatter.locale = latinHebrewLocale
        dateFormatter.dateStyle = .full
        dateFormatter.timeStyle = .none
        return dateFormatter.string(from: date)
    }

    func dateFormattedLT(date: Date) -> String{
        let dateFormatter = DateFormatter()
        dateFormatter.calendar = latinHebrewCalendar
        dateFormatter.locale = latinHebrewLocale
        dateFormatter.setLocalizedDateFormatFromTemplate("yMMMMEEEEd")
        return dateFormatter.string(from: date)
    }

    let formatStyleL = Date.FormatStyle(
        date: .complete,
        time: .omitted,
        locale: latinHebrewLocale,
        calendar: latinHebrewCalendar
    )

}

Date not fully localized with Date.FormatStyle
 
 
Q