To get a browser’s culture in Blazor WebAssembly, call the (navigator.language) property in JavaScript using the JavaScript interop function in Blazor. It will return the current browser’s language version.
Follow this code to get the browser’s culture in Blazor WebAssembly.
[Index.razor]
@page "/"
@inject IJSRuntime JSRuntime
@code{
protected override async Task OnInitializedAsync()
{
var browserLanguage = await JSRuntime.InvokeAsync<string>("getBrowserLanguage");
Console.WriteLine(browserLanguage);
}
}
[index.html]
<body>
. . .
. . .
<script>
window.getBrowserLanguage = function () {
return (navigator.languages && navigator.languages.length) ? navigator.languages[0] :
navigator.userLanguage || navigator.language || navigator.browserLanguage || 'en';
}
</script>
</body>
Share with