Get localized week days

I want to get localized week day. On xCode, my app has only english language for the moment and I set the application region to "French".


When I run this code :

print("\(Calendar.current.locale)")
print("\(Calendar.current.shortWeekdaySymbols)")


I have this output:

Optional(en_FR (current))
["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]


I understand "en_FR" as "en" because my only language is english, and FR is the region. But why shortWeekdaySymbols is not consistent with my FR region?

Answered by Claude31 in 365735022

Then read them


        let prefLanguage = Locale.preferredLanguages[0]

and replace

        calendar.locale = NSLocale(localeIdentifier: "fr_FR") as Locale

with

        let prefLanguage = Locale.preferredLanguages[0]
        var calendar = Calendar(identifier: .gregorian)
        calendar.locale = NSLocale(localeIdentifier: prefLanguage) as Locale

The region is for things like currency symbols and date/time formatting. Language is for language.

Doc states:

https://developer.apple.com/documentation/foundation/calendar/2292848-shortweekdaysymbols

By default, Calendars have no locale set. If you wish to receive a localized answer, be sure to set the

locale
property first - most likely to
Locale.autoupdatingCurrent
.



I did the following:

        var calendar = Calendar(identifier: .gregorian)
        calendar.locale = NSLocale(localeIdentifier: "fr_FR") as Locale
        print(calendar.shortWeekdaySymbols)


To get

["dim.", "lun.", "mar.", "mer.", "jeu.", "ven.", "sam."]

Thanks for the answer. But I'm trying to have a generic solution that use the user phone setting (and not hardcoded).

Accepted Answer

Then read them


        let prefLanguage = Locale.preferredLanguages[0]

and replace

        calendar.locale = NSLocale(localeIdentifier: "fr_FR") as Locale

with

        let prefLanguage = Locale.preferredLanguages[0]
        var calendar = Calendar(identifier: .gregorian)
        calendar.locale = NSLocale(localeIdentifier: prefLanguage) as Locale

Thanks. In Swift 4 :

let prefLanguage = Locale.preferredLanguages[0]
var calendar = Calendar(identifier: .gregorian)
calendar.locale = Locale(identifier: prefLanguage)
This is annoying and unsatisfying because there is no reasonable argument that Calendar.autoupdatingCurrent shouldn't / wouldn't give the properly formatted dates in the current language, and (b) because even accepting that, it still seems to start the week on dimanche rather than lundi like any civilised country would do
Get localized week days
 
 
Q