Live Chat Icon For mobile
Live Chat Icon

How do I localize the text in a Blazor WebAssembly application?

Platform: Blazor| Category : General, WebAssembly

To localize the text in a Blazor WebAssembly (client-side) app, follow these steps:

1. Create a Blazor WebAssembly project and add the Microsoft.Extensions.Localization NuGet package using NuGet Package Manager.

2. Add the culture resource files in the Shared/ResourceFiles folder and add a drop-down menu to switch between localized text.


@using  System.Globalization
@inject IJSRuntime JSRuntime
@inject NavigationManager UrlHelpers

<select class="form.control" @bind="Culture" style="width: 140px; margin-left: 10px;">
    @foreach (var culture in supportedCultures)
        <option value="@culture">@culture.DisplayName</option>

    CultureInfo[] supportedCultures = new[]
        new CultureInfo("en-US"),
        new CultureInfo("es-ES"),
        new CultureInfo("de")

    CultureInfo Culture
        get => CultureInfo.CurrentCulture;
            if (CultureInfo.CurrentCulture != value)
                var jsInvoke = (IJSInProcessRuntime)JSRuntime;
                jsInvoke.InvokeVoid("blazorCulture.set", value.Name);

                UrlHelpers.NavigateTo(UrlHelpers.Uri, true);


<div class="top-row px-4">
      <CultureDropDown />
      . . .

3. Now add the localization configuration and get the current locale’s culture using JSInterop, which is stored in browser window’s local storage.


using Microsoft.JSInterop;
using System.Threading.Tasks;

namespace BlazorLocaleWASM
  public class Program
     public static async Task Main(string[] args)
                var host = builder.Build();
        var jsInterop = host.Services.GetRequiredService<IJSRuntime>();
        var result = await jsInterop.InvokeAsync<string>("blazorCulture.get");
        if (result != null)
          var culture = new CultureInfo(result);
          CultureInfo.DefaultThreadCurrentCulture = culture;
          CultureInfo.DefaultThreadCurrentUICulture = culture;

        await host.RunAsync();



    . . .
    . . .

     . . .
        window.blazorCulture = {
            get: () => window.localStorage['BlazorCulture'],
            set: (value) => window.localStorage['BlazorCulture'] = value


4. Add the text/content for localization in the Index.razor file.


@page "/"
@using BlazorLocaleWASM.Shared.ResourceFiles
@inject Microsoft.Extensions.Localization.IStringLocalizer<Resource> Locale

<h1>@Locale["Hello world"]</h1>

@Locale["Welcome to your new app"]

<SurveyPrompt Title="How is Blazor working for you?" />

5. By default, Blazor WebAssembly carries minimal globalization resources required to display values, such as dates and currency, in the user’s culture. Applications that must support dynamically changing the culture should configure BlazorWebAssemblyLoadAllGlobalizationData in the project file.

[Project file]

<PropertyGroup>        <BlazorWebAssemblyLoadAllGlobalizationData>true</BlazorWebAssemblyLoadAllGlobalizationData>

6. Run the application.

You can download the reference sample here.

Please refer to this link for more information.

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.