Skip to content

Blazor globalization (.NET 11): UseCultureFromServer opt-out sample uses API removed before release; config key and default not documented #38201

Open

Description

@hakenr

The section Client-side prerendering in a Blazor Web App preserves the server's culture (in aspnetcore/blazor/globalization-localization.md and in the ASP.NET Core 11 release notes, aspnetcore/release-notes/aspnetcore-11/includes/blazor.md) shows this opt-out:

builder.Services.AddRazorComponents()
    .AddInteractiveWebAssemblyComponents(options =>
    {
        options.UseCultureFromServer = false;
    });

This API does not exist in the shipped .NET 11.0.0. The public WebAssemblyComponentsOptions class and the AddInteractiveWebAssemblyComponents(Action<WebAssemblyComponentsOptions>?) overload were removed before release by dotnet/aspnetcore#67367 ("Fix culture persistence to not have public API", fixes dotnet/aspnetcore#66829). The Microsoft.AspNetCore.Components.WebAssembly.Server 11.0.0 package contains only the internal WebAssemblyComponentsServiceOptions, so the sample above doesn't compile.

Actual behavior in 11.0.0

  • The setting is configuration-based: Components:UseCultureFromServer (true/1 or false/0). It's read by the internal WebAssemblyComponentsServiceOptionsConfiguration.
  • The default is not simply "on". When the key isn't set, culture capture is enabled only if IStringLocalizerFactory is registered on the server, which is the case when the app calls AddLocalization().
  • On the client, WebAssemblyHost.RunAsyncCore calls CultureStateProvider.ApplyStoredCulture() after user code in Program and before LoadCurrentCultureResourcesAsync().

Why this matters: conflict with the documented client-side culture pattern

The same article's section Dynamically set the client-side culture by user preference recommends reading the culture from local storage and setting CultureInfo.DefaultThreadCurrentCulture/DefaultThreadCurrentUICulture in Program before host.RunAsync().

In a Blazor Web App that:

  • calls AddLocalization() on the server (so capture is on implicitly),
  • prerenders at least one InteractiveWebAssembly component (for example in App.razor, even when Routes uses prerender: false), and
  • doesn't use request localization on the server,

the server's process culture overrides the culture the user selected on the client. Satellite assemblies for the selected culture are never loaded, and the UI stays in the server's language. This silently broke a working language switcher when we upgraded from .NET 10 to .NET 11. Browsers with a non-English language also get the server's culture.

Suggested doc changes

  1. Replace the opt-out sample with the configuration key, for example in appsettings.json:
    {
      "Components": {
        "UseCultureFromServer": false
      }
    }
  2. Describe the actual default: enabled when localization services are registered (AddLocalization), unless the key is set explicitly.
  3. In Dynamically set the client-side culture by user preference, and in any Blazor Web App variant that sets the culture in the client's Program file, add a note for .NET 11+. When the server prerenders interactive WebAssembly components, either set Components:UseCultureFromServer to false, or flow the same culture to the server, for example with a culture cookie and UseRequestLocalization.
  4. Consider a breaking-change or migration note for 10.0 → 11.0.

Related: dotnet/aspnetcore#63144, dotnet/aspnetcore#67367, dotnet/aspnetcore#66829, dotnet/aspnetcore#70510.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions