Skip to content

Conversation

dariatiurina
Copy link
Contributor

Added Culture persistence between Client and Server

Description

This pull request introduces a new mechanism to persist and enforce the server-side culture on the client in Blazor WebAssembly applications. The main feature is the addition of the CultureStateProvider, which captures the current culture during server prerendering and applies it on the client after hydration.

Changes:

  • Introduced the CultureStateProvider class, which captures the current thread's culture and UI culture for persistence and can reapply them on the client side after hydration.
  • Registered CultureStateProvider in relevant project files so it is available for both server and WebAssembly projects.
  • Added the EnforceServerCultureOnClient extension method to IRazorComponentsBuilder, which registers and persists the CultureStateProvider for interactive WebAssembly render mode.
  • Changed the startup flow in WebAssemblyHost so that, after restoring persisted state, the stored culture is applied (if available) before loading culture-specific resources.
  • Added end-to-end tests to verify that the server culture is correctly persisted and restored on the client when enabled, and not persisted by default.
  • Added EnforceServerCultureOnClient to the unshipped PublicAPI.

Fixes #26941

@dariatiurina dariatiurina self-assigned this Aug 6, 2025
@github-actions github-actions bot added the area-blazor Includes: Blazor, Razor Components label Aug 6, 2025
@dariatiurina dariatiurina marked this pull request as ready for review August 6, 2025 12:34
@Copilot Copilot AI review requested due to automatic review settings August 6, 2025 12:34
@dariatiurina dariatiurina requested a review from a team as a code owner August 6, 2025 12:34
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request introduces a culture persistence mechanism for Blazor WebAssembly applications, allowing server-side culture settings to be maintained on the client after hydration. The main purpose is to ensure consistent localization between server prerendering and client-side execution.

  • Adds CultureStateProvider class to capture and restore culture information across server-client transitions
  • Integrates culture restoration into the WebAssembly host startup sequence after state restoration
  • Provides EnforceServerCultureOnClient extension method to enable this feature opt-in

Reviewed Changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/Components/Shared/src/CultureStateProvider.cs New class that captures current culture during prerendering and applies it on the client
src/Components/WebAssembly/Server/src/WebAssemblyRazorComponentsBuilderExtensions.cs Adds EnforceServerCultureOnClient extension method for configuration
src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyHost.cs Integrates culture restoration into startup flow after state restoration
src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyHostBuilder.cs Registers CultureStateProvider for WebAssembly projects
src/Components/test/E2ETest/ServerRenderingTests/InteractivityTest.cs E2E tests verifying culture persistence behavior
Various project files Adds shared source references for CultureStateProvider
Comments suppressed due to low confidence (3)

src/Components/Shared/src/CultureStateProvider.cs:5

  • The CultureStateProvider class should be in a more appropriate namespace. Since it deals with culture persistence and not forms, it should be in Microsoft.AspNetCore.Components.Web or Microsoft.AspNetCore.Components namespace instead of Microsoft.AspNetCore.Components.Forms.
namespace Microsoft.AspNetCore.Components.Forms;

src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyHost.cs:5

  • This using statement is inconsistent with the actual namespace where CultureStateProvider should be located. If the class is moved to the correct namespace, this using statement should be updated accordingly.
using Microsoft.AspNetCore.Components.Forms;

src/Components/WebAssembly/Server/src/WebAssemblyRazorComponentsBuilderExtensions.cs:10

  • This using statement is inconsistent with the actual namespace where CultureStateProvider should be located. If the class is moved to the correct namespace, this using statement should be updated accordingly.
using Microsoft.AspNetCore.Components.Forms;

@ilonatommy ilonatommy added this to the 10.0-rc1 milestone Aug 6, 2025
Copy link
Member

@javiercn javiercn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great.

Mainly we will have to discuss the naming on API review

Copy link
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Aug 15, 2025
@ilonatommy
Copy link
Member

@javiercn, this looks good. Were we waiting for rc2 to do it? Why is it on hold? I thought we API-review only merged PRs

@javiercn
Copy link
Member

javiercn commented Aug 20, 2025

@ilonatommy we punted it to 11.

There are a few tweaks that we want to do that involve public API.

Ideally we do API review as we go, no after the merge.

@dariatiurina dariatiurina requested review from a team and tdykstra as code owners August 21, 2025 09:06
@dariatiurina dariatiurina removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Aug 21, 2025
Copy link
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Aug 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When prerendering, emit culture name and use it on WebAssembly side

3 participants