Blazor.WebAssembly.DynamicCulture 3.0.0

dotnet add package Blazor.WebAssembly.DynamicCulture --version 3.0.0
NuGet\Install-Package Blazor.WebAssembly.DynamicCulture -Version 3.0.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.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Blazor.WebAssembly.DynamicCulture --version 3.0.0
#r "nuget: Blazor.WebAssembly.DynamicCulture, 3.0.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.
// Install Blazor.WebAssembly.DynamicCulture as a Cake Addin
#addin nuget:?package=Blazor.WebAssembly.DynamicCulture&version=3.0.0

// Install Blazor.WebAssembly.DynamicCulture as a Cake Tool
#tool nuget:?package=Blazor.WebAssembly.DynamicCulture&version=3.0.0

Blazor.WebAssembly.DynamicCulture

Nuget Nuget GitHub

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

gif

Samples

  1. 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 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. 
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.0.0 1,486 11/15/2023
3.0.0-preview 109 8/30/2023
2.0.0 437 11/21/2022
1.1.0 321 11/15/2022
1.0.0 382 6/17/2022