Localizer of blazor webassembly no working in zh-hans. RRS feed

  • Question

  • User1052024640 posted

    I followed the sample project in https://github.com/pranavkm/LocSample

    Here is the code of Program.cs:

    using System;
    using System.Net.Http;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    using System.Text;
    using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Logging;
    using Blazored.LocalStorage;
    using System.Globalization;
    using System.Diagnostics;
    using Microsoft.Extensions.Options;
    namespace Temple
    public class Program
    public static async Task Main(string[] args)
    var builder = WebAssemblyHostBuilder.CreateDefault(args);
    builder.Services.AddTransient(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
    builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
    var host = builder.Build();
    var localStorage = host.Services.GetRequiredService<ILocalStorageService>();
    string CurrentLanguage=await localStorage.GetItemAsync<string>("Language"); 
    if (!string.IsNullOrEmpty(CurrentLanguage))
    var culture = new CultureInfo(CurrentLanguage);
    CultureInfo.DefaultThreadCurrentCulture = culture;
    CultureInfo.DefaultThreadCurrentUICulture = culture;
    await host.RunAsync();

    And here is the code of index.razor:

    @page "/"
    @inject IStringLocalizer<Index> Localizer;
    @inject NavigationManager Nav
    @inject Blazored.LocalStorage.ILocalStorageService localStorage
    <button @onclick="ABC">123123</button>
    Welcome to your new app.
    public void ABC()
    localStorage.SetItemAsync("Language", "zh-Hans"); 
    Nav.NavigateTo(Nav.Uri, forceLoad: true);

    And here is the file list:


    As you see, I have using the Blazored.LocalStorage nuget package to achieve the local storage and it works well.

    The problem is when I clicked the button in index.razor. The language never changes to Chinese.

    What's more, I found a strange problem. After I modified the code of void ABC in index.razor like this:

    public void ABC()
    localStorage.SetItemAsync("Language", "en");
    Nav.NavigateTo(Nav.Uri, forceLoad: true);

    When I clicked the button, it changed the language to English correctly.

    What's wrong with this?

    Friday, June 19, 2020 2:26 AM


  • User1052024640 posted

    Problem solved.

    In asp.net core the language name can be either 'zh-hans' or 'zh-Hans' while in Blazor in can only be 'zh-Hans'. It is case sensitivity in Blazor.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 19, 2020 7:54 AM