Charlotte L.
Charlotte L.

Reputation: 173

Swift: show Date() in other language

Is there a way to show date in a language other than English (especially the day in a week) in Swift?

I already have this DateFormatter to show the day in a week, but it only shows it in English:

var today = Date()

static let weekDayFormat: DateFormatter = {
        let formatter = DateFormatter()
        formatter.locale = Locale(identifier: "zh")
        formatter.dateFormat = "E"
        return formatter
    }()

If I write Text("\(today, formatter: Self.weekDayFormat)"), it will show "Sat".

However, I need it to show it in Chinese. Even if I tried set formatter.locale = Locale(identifier: "zh") and other identifier of Chinese, it always output an English result. I think it might just be that the English acronym of weekdays is commonly recognized, but my client wants it to show Chinese.

Upvotes: 4

Views: 6597

Answers (2)

multitudes
multitudes

Reputation: 3505

Localising date depending of the language settings of the iPhone/iPad

This is an interesting case. Did you take the example from Hacking with Swift? ;) https://www.hackingwithswift.com/quick-start/swiftui/how-to-format-text-inside-text-views

I use setLocalizedDateFormatFromTemplate because preferred by Apple as in WWDC: https://developer.apple.com/videos/play/wwdc2020/10160/

preferred date formatter for localisation

I had the same problem, it would not work with the locale parameter inside the static var and I solved it in this way:

import SwiftUI

struct test: View {

    var today: String {
        let date = Date()
        let formatter = DateFormatter()
        formatter.locale = Locale(identifier: "zh")
        //or formatter.locale = .autoupdatingCurrent 
        formatter.setLocalizedDateFormatFromTemplate("E")
        return formatter.string(from: date)
    }

    // or edited from Leo Dabus
    static let weekDay: DateFormatter = {
                    let formatter = DateFormatter()
                    formatter.locale = Locale(identifier: "zh")
                    // or formatter.locale = .autoupdatingCurrent 
                    formatter.setLocalizedDateFormatFromTemplate("E")
                    return formatter
            }()

    var body: some View {
        Text("\(today) \(Self.weekDay.string(from: Date()))")
        }
}

struct test_Previews: PreviewProvider {
    static var previews: some View {
        test()
    }
}

Autoupdate depending of setting

formatter.locale = .autoupdatingCurrent will auto update according to changes to the current language setting of the simulator or device in setting/general/language.
But don't forget to add those languages in your project!

  • In the project navigator (View > Navigator > Show Project Navigator), select your project at the top.
  • In the editor displayed, select your project at the top of the list on the left.
  • Switch to the Info tab.
  • In the Localizations slice, press the add (+) button and select the language from the list.

https://developer.apple.com/forums/thread/97252?answerId=295557022#295557022 Xcode screenshot

Upvotes: 0

Leo Dabus
Leo Dabus

Reputation: 236340

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text(Formatter.weekDay.string(from: Date())).padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

extension Formatter {
    static let weekDay: DateFormatter = {
        let formatter = DateFormatter()

        // you can use a fixed language locale
        formatter.locale = Locale(identifier: "zh")
        // or use the current locale
        // formatter.locale = .current
        
        // and for standalone local day of week use ccc instead of E
        formatter.dateFormat = "ccc"
        return formatter
    }()
}

Upvotes: 7

Related Questions