Blazor.WebAssembly.DynamicCulture 3.2.0

dotnet add package Blazor.WebAssembly.DynamicCulture --version 3.2.0
                    
NuGet\Install-Package Blazor.WebAssembly.DynamicCulture -Version 3.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Blazor.WebAssembly.DynamicCulture" Version="3.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Blazor.WebAssembly.DynamicCulture" Version="3.2.0" />
                    
Directory.Packages.props
<PackageReference Include="Blazor.WebAssembly.DynamicCulture" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Blazor.WebAssembly.DynamicCulture --version 3.2.0
                    
#r "nuget: Blazor.WebAssembly.DynamicCulture, 3.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Blazor.WebAssembly.DynamicCulture@3.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Blazor.WebAssembly.DynamicCulture&version=3.2.0
                    
Install as a Cake Addin
#tool nuget:?package=Blazor.WebAssembly.DynamicCulture&version=3.2.0
                    
Install as a Cake Tool

Blazor.WebAssembly.DynamicCulture

Nuget Nuget GitHub

Dynamic localization support for Blazor WebAssembly applications. This library replicates the functionality of .UseRequestLocalization from Blazor Server for Blazor WebAssembly (WASM), enabling culture switching without page reloads.

πŸ“’ Important: Native Blazor Alternative Available

Blazor now provides a native way to load all satellite assemblies in WebAssembly applications. You can configure this in your index.html:

Before:

<script src="_framework/blazor.webassembly.js"></script>

After:

<script src="_framework/blazor.webassembly.js" autostart="false"></script>
<script>
   Blazor.start({ 
       configureRuntime: runtime => runtime.withConfig({ 
           loadAllSatelliteResources: true 
       }) 
   })
</script>

This native approach may reduce or eliminate the need for this library. However, this library still provides additional features:

  • Built-in culture providers (QueryString, LocalStorage, AcceptLanguageHeader)
  • Automatic component refresh on language change via LanguageTrackProvider
  • Simplified culture management API
  • No manual Blazor startup configuration needed

Evaluate which solution best fits your project requirements.

🎬 Demonstration

Dynamic Culture Demo

πŸ“š Sample Projects

πŸš€ Getting Started

Prerequisites

Add the following property to your .csproj file to load all globalization data:

<BlazorWebAssemblyLoadAllGlobalizationData>true</BlazorWebAssemblyLoadAllGlobalizationData>

Service Registration

Configure services in your Blazor WASM Program.cs:

using Blazor.WebAssembly.DynamicCulture;
using Microsoft.Extensions.Localization;

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");

builder.Services.AddLocalization(); // Requires Microsoft.Extensions.Localization package
builder.Services.AddLocalizationDynamic(options =>
{
    options.SetDefaultCulture("en-US"); // Specify your default culture
    options.AddSupportedCultures("en-US", "et", "ru");
    options.AddSupportedUICultures("en-US", "et", "ru");
});

var host = builder.Build();
await host.SetMiddlewareCulturesAsync();
await host.RunAsync();

⚠️ Note: Do not use this library for Blazor Server applications. Use .UseRequestLocalization instead.

Add Required Imports

Add the following to your _Imports.razor:

@using Microsoft.Extensions.Localization
@using Blazor.WebAssembly.DynamicCulture.Services
@using Blazor.WebAssembly.DynamicCulture

Component Configuration

Add the LanguageTrackProvider component to pages/components that need dynamic culture updates. This component listens for LocalizationService.InvokeLanguageChanged events and calls StateHasChanged on the corresponding component.

For version 1.x.x:

<LanguageTrackProvider Component="this"/>

For version 2.x.x and higher:

<LanguageTrackProvider OnInitializeEvent="provider => provider.RegisterComponent(this)"/>

πŸ”§ Culture Providers

This library includes three built-in culture providers that determine the active culture:

  1. QueryStringCultureProvider - Checks for culture in URL query parameters
  2. LocalStorageCultureProvider - Retrieves culture from browser local storage (key: getBlazorCulture)
  3. AcceptLanguageHeaderCultureProvider - Uses the browser's Accept-Language header

Providers are checked in the order listed above. The first provider that returns a culture is used.

Custom Culture Providers

You can implement custom providers by implementing the ICultureProvider interface and modifying LocalizationDynamicOptions.CultureProviders.

🎨 Creating a Language Selector Component

Create a custom language selector component to allow users to switch languages. This example uses LocalStorageCultureProvider:

@inject LocalizationLocalStorageManager LocalizationLocalStorageManager
@inject ILocalizationService LocalizationService

<MudMenu StartIcon="@Icons.Material.Outlined.Translate" 
         EndIcon="@Icons.Material.Filled.KeyboardArrowDown" 
         Label="@GetAvailableLanguageInfo(Culture).Name" 
         Color="Color.Secondary" 
         Direction="Direction.Bottom" 
         FullWidth="true" 
         OffsetY="true" 
         Dense="true">
    @foreach (var language in _supportedLanguages)
    {
        @if (Equals(Culture, language.Culture))
        {
            <MudMenuItem OnClick="() => OnLanguageClick(language.Culture)">
                <u>@language.Name</u>
            </MudMenuItem>
        }
        else
        {
            <MudMenuItem OnClick="() => OnLanguageClick(language.Culture)">
                @language.Name
            </MudMenuItem>
        }
    }
</MudMenu>

@code {
    private readonly LanguageInfo[] _supportedLanguages = {
        new("English", "English", new CultureInfo("en-US")),
        new("Russian", "Русский", new CultureInfo("ru")),
        new("Estonian", "Eesti", new CultureInfo("et"))
    };

    private CultureInfo Culture => CultureInfo.CurrentUICulture;

    private async Task OnLanguageClick(CultureInfo selectedCulture)
    {
        await SetCulture(selectedCulture);
    }

    private LanguageInfo GetAvailableLanguageInfo(CultureInfo culture)
    {
        foreach (var language in _supportedLanguages)
        {
            if (Equals(culture, language.Culture))
            {
                return language;
            }
        }

        throw new NotSupportedException($"Language with culture '{culture.Name}' is not supported.");
    }

    private async Task SetCulture(CultureInfo cultureInfo)
    {
        CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
        CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
        await LocalizationLocalStorageManager.SetBlazorCultureAsync(cultureInfo.Name);
        LocalizationService.InvokeLanguageChanged(cultureInfo);
    }
}

πŸ“ Usage Example

The following demonstrates how to use localized strings with IStringLocalizer<T>. The Razor markup @Loc["Greeting"] localizes the string keyed to the Greeting value defined in your resource files.

@page "/culture-example"
@inject IStringLocalizer<Translation> Loc

<LanguageTrackProvider OnInitializeEvent="provider => provider.RegisterComponent(this)"/>

<h2>@Loc["Greeting"]</h2>
<p>@Loc["WelcomeMessage"]</p>

πŸ“– Additional Resources

πŸ“„ License

This project is licensed under the MIT License. See the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.2.0 125 1/16/2026
3.2.0-preview1 218 11/26/2025
3.1.0 31,889 7/18/2024
3.0.0 9,985 11/15/2023
3.0.0-preview 287 8/30/2023
2.0.0 676 11/21/2022
1.1.0 503 11/15/2022
1.0.0 794 6/17/2022