Skip to content

Added Culture persistence between Client and Server #63144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

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

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
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