Skip to content

Initialize the root locale for NSHostingView on macOS #968

Description

@Kyle-Ye

Summary

NSHostingView creates an EnvironmentValues instance and passes it to the view graph without applying root environment configuration.

The default locale is Locale(identifier: ""), so Text(_:format:) passes an empty locale to Foundation instead of using the current system locale. This causes TextHeightUITests/dateFormatStyleExample() to fail on macOS.

Current behavior

For the fixed date used by TextFormatStyleExample, OpenSwiftUI renders:

  • 2026-01-01
  • 2026 M01 1, Thu 8:00:00 AM GMT+8
  • 08:00

SwiftUI renders:

  • 1/1/2026
  • Thursday, January 1, 2026 at 08:00:00 GMT+8
  • 08:00

Formatting an equivalent Date.FormatStyle directly with Locale(identifier: "") reproduces the OpenSwiftUI output.

Expected behavior

The macOS root environment should use the current system locale unless a view explicitly overrides it. TextHeightUITests/dateFormatStyleExample() should match the SwiftUI reference.

Implementation notes

  • EnvironmentValues.configureForRoot() initializes locale, calendar, and timeZone.
  • UIKit applies this configuration when constructing its root environment.
  • NSHostingView.updateEnvironment() currently forwards a plain EnvironmentValues() without applying it.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: hosting-bridgeSwiftUI bridge, UIHosting/NSHosting, representables, and platform host views.area: textText, labels, line style, and text rendering APIs.platform: macOSmacOS-specific behavior or support.type: bugSomething is not working correctly.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions