I'm pretty much a newbie when it comes to Blazor. I want to implement multi-language in my Blazor web app(Blazor United) in InteractiveAuto render mode.
I followed this tutorial:https://learn.microsoft.com/en-us/aspnet/core/blazor/globalization-localization?view=aspnetcore-8.0#interactive-auto-components
The tutorial is quite long, so I can't post all my code.
After following everything in the tutorial, the code ran successfully.
However, the default language in my program is not English. I set my default language in advance in this piece of code in program.cs.
var supportedCultures = new[] { "zh-hans","en" }; var localizationOptions = new RequestLocalizationOptions() .SetDefaultCulture(supportedCultures[0]) .AddSupportedCultures(supportedCultures) .AddSupportedUICultures(supportedCultures); app.UseRequestLocalization(localizationOptions); app.MapControllers();And here is my code to test it in MainLayout:
@inherits LayoutComponentBase@inject IStringLocalizer<MainLayout> Localizer@inject Blazored.LocalStorage.ISyncLocalStorageService LocalStorage@code { protected override async Task OnAfterRenderAsync(bool firstRender) { CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("zh-hans"); CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("zh-hans"); } abc = CultureInfo.CurrentCulture.ToString(); } string abc { get; set; } = "456";}<div class="page"><div class="sidebar"><div>@abc</div></div><main><div class="top-row px-4"><a href="https://learn.microsoft.com/aspnet/core/" target="_blank">About</a></div> <article class="content px-4"> @Body</article></main></div><div id="blazor-error-ui" style="display:flex;"> @Localizer["AnUnhandledErrorHasOccurred"]<a href="" class="reload"> @Localizer["Reload"]</a><a class="dismiss">🗙</a></div>But I found a strange problem, when the program is running in Start Debugging mode, everything is normal. However, when I run it in Start Without Debugging mode, the default language is forcibly changed to English.
I even added this piece of code to MainLayout:
protected override async Task OnAfterRenderAsync(bool firstRender){ CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("zh-hans"); CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("zh-hans");}But language still cannot be changed. What causes this? I run it in Start Without Debugging mode for the convenience of using hot reload. How can I solve this problem?