I am working on a blazor server app on .net 6 and in that I have implemented localization from the microsoft documentation here. Also added mudblazor for UI
Project Structure Program.js localization implementation
builder.Services.AddLocalization(opt => opt.ResourcesPath = LocalizationConstants.ResourcesPath);
var localizationOptions = new RequestLocalizationOptions().SetDefaultCulture(LocalizationConstants.SupportedLanguages.Select(x => x.Code).First())
.AddSupportedCultures(LocalizationConstants.SupportedLanguages.Select(x => x.Code).ToArray())
.AddSupportedUICultures(LocalizationConstants.SupportedLanguages.Select(x => x.Code).ToArray());
app.UseRequestLocalization(localizationOptions);
Here I am passing the 2 languages English and Arabic using a class so that code is not hardcoded
I have created a component for selecting the Culture same as given in Microsoft documentation
@using System.Globalization
@using Microsoft.AspNetCore.Mvc;
@inject NavigationManager Navigation
<select @bind="Culture" class="form-select form-select-sm" style="width:fit-content">
@foreach (var culture in supportedCultures)
{
<option class="form-control" value="@culture">@culture.DisplayName</option>
}
</select>
@code
{
private CultureInfo[] supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("ar-iq"),
new CultureInfo("zh-Hans")
};
protected override void OnInitialized()
{
Culture = CultureInfo.CurrentCulture;
}
private CultureInfo Culture
{
get => CultureInfo.CurrentCulture;
set
{
if (CultureInfo.CurrentCulture != value)
{
var uri = new Uri(Navigation.Uri).GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped);
var cultureEscaped = Uri.EscapeDataString(value.Name);
var uriEscaped = Uri.EscapeDataString(uri);
Navigation.NavigateTo($"Culture/Set?culture={cultureEscaped}&redirectUri={uriEscaped}", forceLoad: true);
}
}
}
}
And at last there is a controller which sets the language selected in the cookie so that the state of language selected stays in subsequent requests(also copied from documentation)
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc;
namespace MyProject.Controllers
{
[Route("[controller]/[action]")]
public class CultureController : Controller
{
public IActionResult Set(string culture, string redirectUri)
{
if (culture != null)
{
HttpContext.Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(
new RequestCulture(culture, culture)));
}
return LocalRedirect(redirectUri);
}
}
}
Localization Implementation
@inject IStringLocalizer<App> L
<MudText Typo="Typo.h4" Color="Color.Primary" Class="pt-5">@L["Dashboard"]</MudText>
So when I try to change the language in a page like this - https://localhost:7184/dashboard. There is no issue it works fine but when I try in page like https://localhost:7184/dashboard/Add or like this - https://localhost:7184/dashboard/Edit or https://localhost:7184/dashboard/Add/Count the blazorserver.js and other static files do not load and the UI breaks
I tried debugging the values come fine till the end but it breaks some how and I am not sure what the issue is over here