UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED - Day 40, Project 8, part 2: Date display does not recognize language setting

Forums > 100 Days of SwiftUI

After much searching and help from the Apple Developer forum, I found the answer to the problem below. It appears that I need to change the Development Language from "en" to "nl". This I could do by editing the file "project.pbxproj" inside my app's .xcodeproject file, by changing the line

developmentRegion = en;

to

developmentRegion = nl;

Possibly there is a setting in Xcode that governs this, but I have not found it. Neither have I found a way to make this an Xcode standard, so that I will have to do it for all apps separately. But it works.

====================

Original post:

(Sorry for this lengthy post, but I try to be as complete as possible).

In this day of the project, dates are to be displayed, nicely formatted. For this, the following lines are added.

To Mission.swift:

var formattedLaunchDate: String {
    launchDate?.formatted(date: .abbreviated, time: .omitted) ?? "N/A"
}

and to ContentView.swift:

Text(mission.formattedLaunchDate)

It is then claimed that:

With that change our dates will be rendered in a much more natural way, and, even better, will be rendered in whatever way is region-appropriate for the user – what you see isn’t necessarily what I see.

For me, I did not see the dates as I expected, i.e., they did not follow my local conventions. I looked into it and got more questions than answers.

In Apple's Developer Documentation I read for formatted(date:time:) the following:

Generates a locale-aware string representation of a date using specified date and time format styles.

I understand this as implying that the display of Date objects will follow local conventions, as indicated by the current Locale.

However, I have difficulty in getting this to work correctly.

To analyze the issue, I started with a fresh project, with the following ContentView.swift:

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            Text(Date.now.formatted())
            Text(Date.now.formatted(date: .long, time: .omitted))
            Text(Locale.current.description)
        }
    }
}

I ran it with the Simulator. This gave me a display with the following three lines:

25/5/2022, 9:40 PM

May 25, 2022

en_US (current)

So far, so good. I live in the Netherlands and my Mac is on locale "nl_NL" but apparently Xcode doesn't follow this. Fine.

But formatted() is supposed to follow locale, so I try the following: in the Settings app on the simulated device I set "iPhone language" to "Dutch" and "region" to "Nederland".

Upon restart of the simulation, I see the following:

25/5/2022, 21:44

25 May 2022

en_NL (current)

So, changing language and region has had some effect. The region is now set to NL, and the dates display somewhat differently. But the language is still English: "May" in Dutch should be written "mei". Also, dates are formatted differently in Dutch.

I read somewhere that there are settings in the Scheme for the project that influence behaviour. So I went into Scheme/Run, Options tab, and changed App Language to Dutch and App Region to Netherlands. This gives:

25/5/2022, 21:54

25 May 2022

en_NL (current)

So, no effect.

I finally followed a recipe I found on the Internet and added the following lines to ContentView.swift, just after the import SwiftUI line:

extension NSLocale {
    @objc
    static let currentLocale = NSLocale(localeIdentifier: "nl_NL")
}

Running now gives:

25-5-2022 21:56

25 mei 2022

nl_NL (current)

This is finally how it should be.

Just to be sure, I went back to the Settings on the simulator and reset language to English (US) and region to United States. I commented out the NSLocale extension. It went back to the "en_NL" settings (thus with English for language, but still NL as region).

Finally, I went back to the Scheme and reset language and region to their original settings. Now everything was back to US and English. Adding the extension again set everything back to Dutch.

To me it seems that settings in the Scheme override those on the simulator (makes sense to me), and that extending NSLocale as I did overrides that again. It also seems to me that both Settings and Scheme do not affect the language, only the region.

For completeness, I also connected my iPhone and ran the app there. No difference with the simulator. App would only use Dutch language if forced to to so by the NSLocale extension, regardless of my iPhone settings, which are Dutch and Netherlands.

This leaves me with the following questions:

  • Why is it that changing the region in the Settings app on the simulator has effect, but changing the language isn't picked up?

  • Why do I need to add the extension to NSLocale when Date.formatted() is supposed to follow locale anyway? Should setting the locale (language/region) in the simulator not be sufficient?

  • Is it safe to use the NSLocale extension? Does it only affect the current app, or could it have broader impact?

  • If I really need the extension, is there a pure SwiftUI alternative?

Do others have the same experience? Am I missing something obvious? Any insights will be greatly appreciated!

2      

In Xcode check your Run settings by going

  1. Under the Product menu item, press Option so that you get the alternative commands and select Run (So Product\Option-Run).
  2. Select the Options column (from Info, Arguments, Options, Diagnostics)
  3. Check that the App Region is set to System Region
  4. Also check that the App Language is set to System Language
  5. Then press Close (bottom right)

2      

Thanks for replying.

I did as you suggested. It made no difference. Details follow.

Product\Option Run settings showed Dutch/Netherlands. I reset them to System Language/System Region.

The settings in the simulator are still Dutch/Netherlands. The Locale extension is commented out. Running the simulation gives:

26/5/2022, 3:42

26 May 2022

en_NL (current)

Then setting the settings in the simulator to English/United States gives

5/26/2022, 3:47 AM

May 26, 2022

en_US (current)

Reactivating the extension gives again the desired result:

26-5-2022 3:50

26 mei 2022

nl_NL (current)

I rechecked the Product\Option Run settings, still System Language/System Region.

In summary, no difference it seems.

Just to be sure, I change the settings in the simulator by pressing the home button (simulating an iPhone 8) and going to Settings, General, Language & Region. Then I restart the simulation from XCode by cmd-R plus Replace.

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.