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
<PackageReference Include="Blazor.WebAssembly.DynamicCulture" Version="3.2.0" />
<PackageVersion Include="Blazor.WebAssembly.DynamicCulture" Version="3.2.0" />
<PackageReference Include="Blazor.WebAssembly.DynamicCulture" />
paket add Blazor.WebAssembly.DynamicCulture --version 3.2.0
#r "nuget: Blazor.WebAssembly.DynamicCulture, 3.2.0"
#:package Blazor.WebAssembly.DynamicCulture@3.2.0
#addin nuget:?package=Blazor.WebAssembly.DynamicCulture&version=3.2.0
#tool nuget:?package=Blazor.WebAssembly.DynamicCulture&version=3.2.0
Blazor.WebAssembly.DynamicCulture
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
π Sample Projects
- Blazor.WebAssembly.Sample.DynamicCulture - Complete sample demonstrating basic usage and features
π 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
.UseRequestLocalizationinstead.
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:
- QueryStringCultureProvider - Checks for culture in URL query parameters
- LocalStorageCultureProvider - Retrieves culture from browser local storage (key:
getBlazorCulture) - 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 | Versions 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. |
-
net10.0
- Blazor.WebAssembly.DynamicCulture.Loader (>= 3.2.0)
-
net6.0
- Blazor.WebAssembly.DynamicCulture.Loader (>= 3.2.0)
-
net7.0
- Blazor.WebAssembly.DynamicCulture.Loader (>= 3.2.0)
-
net8.0
- Blazor.WebAssembly.DynamicCulture.Loader (>= 3.2.0)
-
net9.0
- Blazor.WebAssembly.DynamicCulture.Loader (>= 3.2.0)
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 |