Blazor.WebAssembly.DynamicCulture
3.1.0
dotnet add package Blazor.WebAssembly.DynamicCulture --version 3.1.0
NuGet\Install-Package Blazor.WebAssembly.DynamicCulture -Version 3.1.0
<PackageReference Include="Blazor.WebAssembly.DynamicCulture" Version="3.1.0" />
paket add Blazor.WebAssembly.DynamicCulture --version 3.1.0
#r "nuget: Blazor.WebAssembly.DynamicCulture, 3.1.0"
// Install Blazor.WebAssembly.DynamicCulture as a Cake Addin #addin nuget:?package=Blazor.WebAssembly.DynamicCulture&version=3.1.0 // Install Blazor.WebAssembly.DynamicCulture as a Cake Tool #tool nuget:?package=Blazor.WebAssembly.DynamicCulture&version=3.1.0
Blazor.WebAssembly.DynamicCulture
This library essentially replicates the functionality of .UseRequestLocalization
for Blazor Server-Side(BSS), but it is specifically designed for Blazor WebAssembly (WASM). It relies on the default IStringLocalizer
and utilizes the Blazor.WebAssembly.DynamicCulture.Loader, eliminating the need to refresh the page when switching languages.
Demonstration
Samples
- Blazor.WebAssembly.Sample.DynamicCulture - Minimal getting started project. Shows basic usage of
Blazor.WebAssembly.DynamicCulture
.
Getting Started
Add to .csproj
<BlazorWebAssemblyLoadAllGlobalizationData>true</BlazorWebAssemblyLoadAllGlobalizationData>
Register Services
Blazor WASM
builder.Services.AddLocalization(); //requires Microsoft.Extensions.Localization package
builder.Services.AddLocalizationDynamic(options =>
{
options.SetDefaultCulture("en-US"); //Do not forget to specify your delfault culture, usually the neutral one is en-US
options.AddSupportedCultures("et", "ru");
options.AddSupportedUICultures("et", "ru");
});
//...
var host = builder.Build();
await host.SetMiddlewareCulturesAsync();
await host.RunAsync();
NB! Do not use it for Blazor ServerSide.
Add Imports
After the package is added, you need to add the following in your _Imports.razor
@using Microsoft.Extensions.Localization
@using Blazor.WebAssembly.DynamicCulture.Services
@using Blazor.WebAssembly.DynamicCulture
Add Components
Add the following for each components / pages that needs dynamic cultures. It will listen for LocalizationService.InvokeLanguageChanged
and call StateHasChanged
for the corresponding component.
For version 1.x.x:
<LanguageTrackProvider Component="this"/>
For version 2.x.x and higher:
<LanguageTrackProvider OnInitializeEvent="provider => provider.RegisterComponent(this)"/>
Create your own LangugeSelector Component (optional, depending on your needs)
This can be optional in case you don't want to have a language selector and want to take the langauge from query or header for example. In fact this library has 3 providers:
- QueryStringCultureProvider
- LocalStorageCultureProvider
- AcceptLanguageHeaderCultureProvider
First it will check if there is query parameter in url with the culture, then it will check local storage if there is getBlazorCulture
and then it will check for langauge header.
You can make own with implementing ICultureProvider and changing the LocalizationDynamicOptions.CultureProviders.
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("Estonia", "Eesti", new CultureInfo("et"))
};
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.Name} is not supported.");
}
private async Task SetCulture(CultureInfo cultureInfo)
{
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
await LocalizationLocalStorageManager.SetBlazorCultureAsync(cultureInfo.Name);
LocalizationService.InvokeLanguageChanged(cultureInfo);
}
private CultureInfo Culture => CultureInfo.CurrentUICulture;
}
Page example
The following demonstrates the use of the localized Greeting string with IStringLocalizer<T>. The Razor markup @Loc["Greeting"] in the following example localizes the string keyed to the Greeting value, which is set in the preceding resource files.
@page "/culture-example-2"
@* Translation - resx class with translations *@
@inject IStringLocalizer<Translation> Loc
<LanguageTrackProvider Component="this"/>
<h2>Loc["Greeting"]</h2>
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. |
-
net6.0
- Blazor.WebAssembly.DynamicCulture.Loader (>= 3.1.0)
-
net7.0
- Blazor.WebAssembly.DynamicCulture.Loader (>= 3.1.0)
-
net8.0
- Blazor.WebAssembly.DynamicCulture.Loader (>= 3.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.